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 differentiate 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
|Basis for comparison||C++||Java|
|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 may exist 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. It is hybrid languages in some entities are object types, and some are traditional primitive types. It is sometimes inconsistent with pure OO programming such as free-standing functions, because of its origin. However, syntax and semantics remain consistent across native and object data types.
C++ supports generalization with inheritance and runtime method resolution and permits 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 compiler statically allocates storage for objects (primitive or class types) declared at compile time. 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 popularity comes from its portability and its huge library of associated classes. Java compiles source code to an intermediate bytecode, which in turn 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 is a strongly typed language, with distinctive primitive and object types. 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.
All java object types are at the most abstract level because they share a single Object ancestor, type compatible. Run-time type checking throws exceptions if invalid downcasts are used. Polymorphism is automatic, and it can be prevented only by explicitly prohibiting the definition of overriding methods. Only single inheritance is supported by java, although interfaces can emulate multiple superclasses.
Packages are responsible for organizing the classes and providing scope for identifiers. Similar to C++’s access specifiers, Java’s access modifiers restrict the availability of attributes and methods, private provides intraclass access, public provides universal access and protected extends access to subclasses defined outside 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. All Java code (data and methods) exists 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 offers runtime memory error detection and throws appropriate exceptions on detection.
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++ supports 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 behavior at runtime. In contrast, system is responsible for detection of runtime errors in Java which results in compile time or runtime termination.
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 provides a platform for fine granularity of expression, uniform semantics and control with simplicity.
On the other hand, Java emphasizes runtime portability and code safety at the expense of some efficiency and flexibility. Java provides abstract interfaces to imitate multiple inheritance, differentiate between object and primitive type semantics. It also offers a rich library of objects for general and distributed platform implementation.