TechWriterDev
  • Cloud
    • AWS
      • 00_Doubts
      • CloudPractitioner
        • Cloud Computing
        • AWS Global Infrastructure
        • Introduction to AWS EC2
        • Elastic load balancer(ELB)
        • 04_Messaging_Queuing
        • Aditional Computing Service
        • Accessing AWS resources
        • AWS Networking
        • Storage
        • Amazon Database Solutions
        • Monitoring Tools
        • AWS Security
        • Distributed Denial Of Service Attacks
      • DeveloperAssociate
        • References
        • AWS DVA-C02 Services Index
        • Services
          • 00_IAM
            • Identity and Access Management (IAM)
            • Account Protection Mechanisms
            • Access Mechanism of AWS Resources
            • Security Tools
            • Responsibility Model
            • Advanced Concepts
          • 01_EC2
            • Elastic Compute Cloud (EC2)
            • EC2 Volume Types
            • Amazon Machine Image (AMI)
            • AWS charges for IPv4 address
          • 02_SecurityGroups
            • Security Groups
          • 03_Elastic_LoadBalancing
            • Terminology
            • Elastic load balancer
            • Features
            • Basics
          • 04_AutoScaling
            • Auto Scaling
          • 05_RDS
            • Relational Database Service (RDS)
            • Aurora
            • Security
            • RDS Proxy
          • 06_ElastiCache
            • Cache
            • Cache Offerings
          • 07_Route53
            • Basics of DNS
            • Route 53
          • 08_VPC
            • Virtual Private Cloud (VPC)
          • 09_S3
            • Simple Storage Service (S3)
            • S3 Features
            • S3 Encryption
            • S3 Features
            • S3 Bucket Policy and IAM Policy
          • 10_ECS
            • Elastic Container Service (ECS)
            • Elastic Container Registry (ECR)
            • AWS Copilot
          • 11_EKS
            • Elastic Kubernetes Service (EKS)
          • 12_SDK_CLI_Tips
            • Access AWS Resources
          • 13_CloudFront
            • Cloud Front
          • 14_Messaging
            • Simple Queue Service (SQS)
            • Simple Notification Service (SNS)
            • Fan Out Pattern
            • Kinesis
            • Compare and Contrast
          • 15_ElasticBeanStalk
            • Elastic Beanstalk
          • 16_CloudFormation
            • CloudFormation
            • Dynamic References
          • 17_Monitoring
            • AWS Monitoring
            • AWS CloudWatch
            • CloudWatch Alarms
            • Synthetics Canary
            • Amazon EventBridge (formerly CloudWatch Events)
            • X-Ray
            • OpenTelemetry
            • CloudTrail
          • 18_Lambda
            • Lambda
            • Lambda Integrations
            • Configuring Lambda
            • Lambda Layers
          • 19_API_Gateway
            • API Gateway
            • API Gateway Integrations
          • 20_DynamoDB
            • DynamoDB
            • Operations
            • Indexes
            • DynamoDB Accelerator (DAX)
            • DynamoDB Streams
            • Transactions
            • Integrations
          • 21_CICD
            • CICD
            • CodeCommit
            • CodePipeline
            • CodeBuild
            • CodeDeploy
            • CodeArtifact
            • CloudGuru
          • 22_SAM
            • Serverless Application Model (SAM)
          • 23_CDK
            • Cloud Development Kit (CDK)
          • 24_StepFunctions
            • Step Functions
            • Types of step function
          • 25_AppSync
            • AppSync
          • 26_Amplify
            • Amplify
          • 27_STS
            • Security Token Service (STS)
          • 28_DirectoryService
            • Active Directory
          • 29_KMS
            • Encryption
            • KMS API
            • Features
            • Cloud Hardware Security Module (HSM)
          • 30_SSM_Store
            • SSM Parameter Store
          • 31_SecretsManager
            • Secrets Manager
          • 32_Cognito
            • Cognito
      • Questions
        • AWS_Region
        • EC2
        • IAM
  • Database
    • MongoDb
      • Mongo db Basics
      • Mongo DB Atlas
      • Document
      • Import-Export based on Data Format
      • Mongo Shell Commands
      • Query Operators
      • Indexes
      • Upsert
      • MongoDB Aggregation Framework
      • Aggregation Framework Operators
    • PostgreSQL
      • POSTGRE SQL DataTypes
      • About table
      • Constraints
  • Technologies
    • RabbitMQ
      • RabbitMQ Concepts
      • Introduction to Exchanges
      • Introduction to Queues
    • Terraform
      • 00_Introduction
      • Configuration blocks
      • Commands
      • Variables
      • Terraform Cloud
      • Modules
  • Languages
    • Java
      • Logging
        • Getting Started
      • 00_Core
        • 00_Basics
          • Java Vs C++
          • Object oriented principles
          • Steps to compile a java program
          • JVM Internals
          • Understanding Java Development Kit
          • What is JIT Compiler?
          • Java data types
          • 07_identifiers_type_conversion
          • 08_references_and_packages
          • Steps for attaching scanner
        • Concurrency
          • 00_Threads
            • Threads
          • 01_ExecutorFramework
            • Executor Framework
            • Asynchronous Computation
      • 01_Backend
        • 01_HttpAndWebServerBasics
          • HTTP
          • Content Type
          • Web Server
        • 02_J2EE_Basics
          • J2EE_Basics
          • Why HttpServlet classs is declared as abstract class BUT with 100 % concrete functionality ?
        • 03_TomCatAndSession
          • What is a Session?
          • WebContainer
        • 04_PageNavigation
          • Cookies Additional Information
          • Page Navigation Techniques
        • 05_AboutServlet
          • CGI v/s Servlet
          • Executor Framework
          • Servlet Life cycle
          • SERVLET CONFIG
          • Servlet Context
          • Servlet Listener (web application listener)
        • 08_SpringBoot
          • Spring Boot
          • Some common annotations used in spring eco system
        • 09_SpringDataJPA
          • Spring Data JPA
        • Java_Language_Changes
          • JDK enhancement tracking reference
        • 06_ORM_Hibernate
          • readmes
            • Hibernate
            • Advantages of Hibernate
            • Hibernate Caching
            • Hibernate API
            • Hibernate Query API
            • Hibernate Annotations and JPQL
            • Entity and Value Type
        • 07_SpringFramework
          • bean_validation
            • Bean Validation
          • core
            • readme
              • Spring
              • Spring Framework Modules
              • Spring MVC Request flow
              • Dependency Injection
              • Spring Beans
              • 06_Spring_Framework_Annotations
      • 03_Tools
        • Maven
          • Maven
  • SoftwareEngineering
    • DesignPatterns
      • Notes
        • Basics
        • OOP
        • SOLID Principles
        • 03_Creational
          • Abstract Factory (aka Kit)
          • Builder
          • Factory Method (aka Virtual constructor)
          • Prototype
          • Singleton
        • 04_Structural
          • Adapter (aka Wrapper)
          • Bridge (aka Handle | Body)
          • Composite
          • Decorator (aka Wrapper)
          • Facade
          • Flyweight
          • Proxy (aka Surrogate)
        • 05_Behavioral
          • Chain of Responsibility
          • Command (aka Action | Transaction)
          • Iterator (aka Cursor)
          • Observer (aka Publish-Subscribe | Dependents)
          • Strategy (aka Policy)
    • Principles
      • REST
        • REST
  • Tools
    • Containers
      • Docker
        • Docker
        • Docker Image
        • Commands
        • Compose
        • Best Practices
      • Kubernetes
        • Kubernetes
    • VCS
      • Git
        • Quick reference of useful Git commands
Powered by GitBook
On this page
  • Development wise differences
  • Syntactical differences
  • Features wise differences.
  1. Languages
  2. Java
  3. 00_Core
  4. 00_Basics

Java Vs C++

Development wise differences

  • Java is platform independent language, but c++ is dependent upon operating system.

  • At compilation time Java Source code .java converts into byte code .class.The interpreter translates this byte code at run time into native code and gives output.

  • Java uses both a compiler and interpreter, while C++ only uses a compiler

Syntactical differences

  • There is no final semi-colon at the end of the class definition.

  • Functions are called as methods.

  • main method is a member of class & has a fixed form

  public static void main(String[] args)
  • Argument is an array of String. This array contains the command-line arguments.(args[0] is not program name as in c++).

  • main method must be inside some class (there can be more than one main function per class).

  • Like the C++ << operator,to write to standard output, you can use either of the following:

    System.out.println( ... ) // prints the given expression followed by a newline
    System.out.print( ... ) // prints the given expression, no new line at end
    • These functions can be used to print values of any type. eg :

      System.out.print("hello");   // print a String
      System.out.print(16);        // print an integer
      System.out.print(5.5 *.2);  // prints a floating-point number
  • The + operator can be useful when printing, it is overloaded to work on strings as follows:

    • If either operand is a String, it converts the other operand to a String (if necessary)

    • Creates a new String by concatenating both operands.

Features wise differences.

  1. C++ supports pointers whereas Java does not support pointer arithmetic. It supports Restricted pointers.

    • Java references (Restricted pointers) can't be arithmetically modified.

  2. C++ supports operator overloading, multiple inheritance but java does not support either.

  3. C++ is nearer to hardware than Java.

  4. Everything (except fundamental or primitive types) is an object in Java (Single root hierarchy as everything gets derived from java.lang.Object).

  5. Java is similar to C++ but it doesn't have the complicated aspects of C++, such as pointers, templates, unions, operator overloading, structures, etc.

  6. Java also does not support conditional compilation (#ifdef / #ifndef type).

  7. Thread support is built into Java but not in C++. From C++11, it does have thread support though.

  8. Internet support is built into Java, but not in C++. On the other hand, C++ has support for socket programming which can be used.

  9. Java does not support header files and library files. Java uses import to include different classes and methods.

  10. Java does not support default arguments.

  11. There is no scope resolution operator :: in Java. It has .(dot) using which we can qualify classes with the namespace they came from.

  12. There is no goto statement in Java.

  13. Because of the lack of destructors in Java, exception and automatic garbage collector handling is different than C++.

  14. Java has method overloading, but no operator overloading like C++.

  15. The String class does use the + and += operators to concatenate strings and String expressions use automatic type conversion,

  16. Java uses pass-by-value.

  17. Java does not support unsigned integers.

  18. In java, all instance methods of a class are virtual by default.


Why java doesn't support c++ copy constructor?

  • Java does. They're just not called implicitly like they are in C++ .

Firstly, a copy constructor is nothing more than:

  public class Blah {
    private int foo;

    public Blah() { } // public no-args constructor
    public Blah(Blah b) { foo = b.foo; }  // copy constructor
}
  • Now C++ will implicitly call the copy constructor with a statement like this:

  Blah b2 = b1;
  • Cloning/copying that instance simply makes no sense in Java because all b1 and b2 are references and not value objects like they are in C++. In C++ that statement makes a copy of the object's state. In Java it simply copies the reference. The object's state is not copied so implicitly calling the copy constructor makes no sense.


  • All stand-alone C++ programs require a function named main and can have numerous other functions.

  • Java does not have stand alone functions, all functions (called methods) are members of a class.

  • All classes in Java ultimately inherit from the Object class, while it is possible to create inheritance trees that are completely unrelated to one another in C++.

  • In this sense , Java is a pure Object oriented language, while C++ is a mixture of Object oriented and structure language.

  • The interface keyword in Java is used to create the equivalence of an pure abstract base class containing only method declarations and constants. No variable data members or method definitions are allowed(true till Java 8) .

  • C++ does not support interface concept.

  • Java does not support multiple inheritance. To some extent, the interface feature provides the desirable features of multiple inheritance to a Java program without some of the underlying problems.(death of a diamond)

  • Java is running on a Virtual Machine, which can recollect unused memory to the operating system, so Java does not need a destructor.

  • Unlike C++, Java cannot access pointers to do memory operation directly. This leads to a whole host of subtle and extremely important differences between Java and C++.

  • Furthermore, the C++ compiler does not check whether all local variables are initialized before they are read. It is quite easy to forget initializing a variable in C++. The value of the variable is then the random bit pattern that happened to be in the memory location that the local variable occupies.

  • Java does not have global functions and global data.

  • Static in Java is just like global in C++, can be accessed through class name directly, and shared by all instances of the class. For C++, static data members must be defined out side of class definition, because they don't belong to any specific instance of the class.

  • Generally Java is more robust than C++ because,

    • Object handles (references) are automatically initialized to null.

    • Handles are checked before accessing, and exceptions are thrown in the event of problems.

    • You cannot access an array out of bounds.

    • Memory leaks are prevented by automatic garbage collection.

    • While C++ programmer clearly has more flexibility to create high efficient program, they can also lead to more chances to encounter error.

Previous00_BasicsNextObject oriented principles

Last updated 3 months ago