Differences Between C++ and Java

Java and C++ both are object-oriented programming languages. Both languages provide object-oriented features such as Classes, Objects, Encapsulation, Data Abstraction, etc.

But C++ is considered as a partially object-oriented programming language because it supports both procedures oriented features and object-oriented features too.

Some differences between Java and C++ are as follows.

Platform-independent

C++ is platform-dependent but Java is platform-independent. This is one of the major features of Java that A Java class can be compiled in a machine but can be run in any machine.

Multiple inheritances

Java doesn't support multiple inheritances through the class but C++ supports multiple inheritances. With the help of Interfaces, we can achieve Multiple inheritances in Java. Virtual is a keyword in C++ which is used to resolve ambiguity among classes during multiple inheritances. This is the main difference between Java and C++.

Exception Handling

With the help of the try-catch block, we can handle the exception at runtime. In Java, there are no destructors, so exception handling is different in Java. In C++ we do not include try catch even if the function throws an exception.

Structure and Union

Java doesn't support structures and unions but C++ supports structures and unions.

Operator Overloading and Method Overloading

C++ supports operator overloading and method overloading both but Java doesn't support operator overloading. Java support method overloading only.

C++ also supports pointer

Like C++ Java does not have any concept of pointers because like C, C++ we have to manage the memory management by destructors. In Java, the Automatic Garbage Collector works the same. This feature makes Java more secure because pointers point to the exact memory location that means we can lose the security as it is using them directly.

Thread Support

Java has built-in support for the thread as Thread is a class in Java and can be used to create and manage the life cycle of a thread. In C++ there is no built-in library, we have to be dependent on third-party libraries for thread support

Goto Statement

C++ supports goto statement but Java does not have a goto statement. Although goto and const are the keywords in Java but using these keywords is not advised. There may be chances that it causes an unconditional loop.
These are some common differences between CPP and java.

Share