The syntax of Java is similar to C++ but there are many differences between C++ and Java.
The major difference between Java and C++ is that C++ is only a compiled language while Java is both compiled as well as interpreted. A C++ compiler converts the source code directly into platform specific machine code therefore it is platform dependent. Whereas, a Java compiler converts Java source code into bytecode and following that, the Java interpreter executes this bytecode at runtime. Java is platform independent because it is interpreted.
Here are the major differences between C++ and Java.
Difference Between Java and C++
Java | C++ |
---|---|
Java is purely Object-oriented programming language, since it is not possible to write a Java program without using at least one class. | C++ is not a purely object-oriented programming language. It is object oriented extension of C. It is possible to write C++ program without using a class or an object. |
Java does not support pointers. | C++ support pointers. |
Java doesn’t support Global variables. | Global variables can be declared in C++. |
Java support automatic garbage collection. However java does provide finalize method. | C++ support destructor functions, which gets called automatically whenever an object is destroyed. |
Java doesn't support multiple inheritance but there are other means to achieve it. | C++ support multiple inheritance. |
Java does not support conditional inclusion and compilation. | C++ supports conditional inclusion and compilation using #ifdef, #ifndef etc. |
Java does not support operator overloading, structures, union and templates. | C++ supports operator overloading, structures, union and templates. |
Java does not support goto jump statement. | C++ supports goto jump statement. Although, using goto is considered as bad programming practice. |
Java is platform independent. Because java is interpreted, same java program can run in multiple platform having JVM. | C++ is platform dependent. Same C++ code may not run on different platforms. |
Java doesn't support default argument to a function. | C++ supports default argument to a function. |
Java provides inbuilt support for documentation comments. Java provides a tool called javadoc, which reads all documentation comments from source code and convert it to HTML format. | C++ does not provide inbuilt support for documentation comments. |
Java provides inbuilt support for multi threading. | C++ does not have inbuilt support for multi threading. |
In Java, function arguments are always passed using call by value. There is support for call by reference. | C++ supports both call by value and call by reference. |
For beginner programmers, it would be better to learn C++ first as the syntax of C++ is easier compared to Java and there is extensive inbuilt support for standard data structures in C++ like queue, stack etc. However, for experienced programmers, Java is a great programming language to know about as it is widely used in multiple large tech companies and therefore it would help the programmers a lot in cracking tech interviews.