C++ and Java are most commonly used programming languages. Java has a strong influence of C++ as it was developed after C++ and both support OOP (Object Oriented Programming) paradigms. The crucial difference which differentiates both of the programming languages is that C++ is platform dependent while Java is platform independent.
Java source code is converted into bytecode when compiled. At runtime, the interpreter executes this bytecode and gives output. Mostly Java is an interpreted language and therefore platform independent. On the other hand, C++ uses a compiler to compile and run the source code. It converts source code into machine level language; hence C++ is platform dependent.
Content: C++ Vs Java
Comparison Chart
Basis for comparison | C++ | Java |
---|---|---|
Platform dependency | Dependent | Independent |
Memory management | Accessible to programmer | System controlled |
Inheritance model | Single and multiple inheritance, | Single inheritance with abstract interfaces. |
Polymorphism | Explicit per method, encourages mix-in hierarchies. | Automatic |
Access control and object protection | Thorough and flexible model with constant protection available. | Cumbersome model encourages weak encapsulation. |
Portability | Source must be recompiled for platform, hence code is not portable. | Bytecode classes portable to platform specific JVM's. |
Type semantics | Consistent between primitive and object types. | Differs for primitive and object types. |
Program organisations | Functions and data can reside external to any class, global (file) and namespace scopes available. | All function and data exist within classes, package scope available. |
Libraries | Predominantly low-level functionality | Massive, classes for high-level services |
Runtime error detection | Programmer responsibility | System responsibility |
Supports | Pointers, structure and union. | Threads and interfaces. |
Definition of C++
C++ was developed with the intent of extending the procedural C language to include OO capabilities. C++ language syntax is the superset of C. In C++ certain units are object type, and several are traditional primitive types, hence it a hybrid language. It is sometimes inconsistent with pure OO programming such as free-standing functions, because of its origin. Nevertheless, syntax and semantics remain stable across native and object data types.
Generalization with inheritance is assisted by C++ and runtime method resolution and allows multiple inheritance. Classes may include combination of polymorphic and non-polymorphic methods. A superclass must explicitly declare the particular method as virtual to enable runtime resolution for that method. This implementation is typically achieved by having each object contain a pointer to a table of the method to its class. It does not have shared basic object type as characterizes other OO languages.
Method and function overloading is supported by C++ where they share the same name but have parameters that vary in number or type. Access specifiers are used to promote encapsulation by restricting the availability of class members (methods or data) in three ways: to methods of the class itself (private), the class and its subclasses (protected), or any class/method/function (public). All the restrictions are performed on classes no on the objects. C++ namespace provides a semantic scope for symbols but doesn’t affect the accessibility of visible entities.
Memory management in C++
C++ exposes its memory management to the programmer, which facilitates customizable memory allocation strategies. The objects (primitive or class types) that are declared at compile time assigned with static storage by the compiler. The new operation is used for obtaining dynamic storage from the heap at runtime which should be explicitly deleted using delete operation while not used. Memory addresses remain fixed for the lifetime of an object. It does not offer run-time error detection facilities.
Definition of Java
Java is also an object-oriented programming language and every java program is defined as a class. It is portable and device- independent language. After that, restricted java program was incorporated into web browsers to allow dynamic and interactive contents on web pages. Java’s prevalence comes from the enormous library of associated classes and its portability. The source code is compiled to an intermediate bytecode by java compiler, which in succession runs on a platform-specific Java Virtual Machine. JVMs are readily available for all operating systems and substitutable with JVM’s from other vendors.
Java also has distinctive primitive and object types and is a strongly typed language. It does not reference syntax for primitives but does provide tools for converting primitives and corresponding object types, for example, int and integer. Some versions of Java also include autoboxing, where the primitives and their corresponding wrappers, are automatically converted.
Every java object types are at the extreme abstract level, the reason is that they share type compatibility with a single Object ancestor. If false downcasts are used the exceptions is thrown by Run-time type checking. Polymorphism is automatic, and it can be prevented when the definition of overriding methods are explicitly prohibited. Only single inheritance is supported by java, even though interfaces can emulate multiple superclasses.
Packages are responsible for organizing the classes and providing scope for identifiers. Same as C++ access specifiers, Java access modifiers restrict the availability of attributes and methods, private provides intraclass access, public provides universal access and protected extends access to subclasses defined exterior to their parent class’s package. Although, java meaning of protected is a bit different than C++.
Memory management in Java
Memory management is the province of the JVM. The Java code (data and methods) remains within the context of a class. A garbage collector is used for object deallocation, the system returns object memory to its pool when the object is not useful anymore. Java also provides runtime memory error detection and when detected it throws appropriate exceptions.
Key Differences Between C++ and Java
- C++ is platform dependent whereas Java is platform independent.
- In C++ objects are located at fixed addresses and memory is managed by programmer. In contrast, Java has a system controlled memory management and objects are relocatable in memory.
- C++ assist single and multiple inheritance while Java does not support multiple inheritance rather abstract interfaces are used.
- Java provides automatic polymorphism. As against, in C++ the polymorphism is explicit for each particular method.
- There is no universal base class in C++ whereas in Java there exist universal object ancestor.
- Java code is portable while C++ does not provide portability.
- The semantics in C++ are consistent between primitive and object types. On the contrary, in java there is a difference in semantics of primitive and object types.
- All function and data exist within classes in Java. Conversely, C++ allows functions and data to be external to any class.
- Java libraries provide massive classes for high-level services and system integration while C++ libraries provide low-level functionality, but rich generic (template) container (data structure) and algorithm.
- To detect runtime errors, a programmer is responsible, which results in undefined behaviour at runtime. In contrast, system is responsible for detection of runtime errors in Java which results in compile time or runtime termination.
Conclusion
Both C++ and Java programming languages support OOP paradigms but there exist nuances and differences between them. C++ is a flexible language emphasizes on the runtime efficiency and can form broad type hierarchies. It is a platform offered for maintaining uniform semantics, fine granularity of expression, and control with simplicity.
On the other hand, Java emphasizes security and portability at the cost of efficiency and flexibility. Java provides abstract interfaces to imitate multiple inheritance, differentiate between object and primitive type semantics. It also offers a huge library of objects for general and distributed platform implementation.
Pushkar says
Interesting blog. I have read your blog and its very much informative providing core insightful stuff. Thanks for sharing the difference between C++ and Java. Great information in this article good and keep it up.
Anil says
Your article is among the best and most informative.