Java vs C++

Similarities Between Java and C++

They are both very popular and successful programming languages. There are various ways in which both of these differ, but there are also various similarities between them. Here are some of them:

  • Both the languages support object-oriented programming.
  • They have the very same type of syntax.
  • The comments syntax is identical between Java and C++.
  • The conditional statements (such as switch, if-else, etc.) and loops (such as for, while etc.) are similar among them both.
  • The relational and arithmetic operators are the same for both of these.
  • The execution of both of these programs begins from the main function.
  • The primitive data types are the same in both.
  • They both have various similar types of keywords.
  • They have multi-threading support.
  • They have pretty similar areas of applications.

 

Difference Between Java and C++

 

Java

C++

Java is platform-independent.
Once compiled into byte code, it can be executed on any platform.

C++ is a platform dependent language.
The source code written in C++ needs to be compiled on every platform.

Java is a compiled as well as an interpreted language.
The compiled output of a Java source code is a byte code which is platform-independent.

C++ is a compiled language.
The source program written
in C++ is compiled into an object code which can then be executed to produce an output.

Java, however, translates the code into byte code.
This byte code is portable and can be executed on any platform.

C++ code is not portable.
It must be compiled for each platform.

C++ code is not portable.
It must be compiled for each platform.

Memory management in C++ is manual.
We need to allocate/deallocate memory manually using the new/delete operators.

In Java, only method overloading is allowed.
It does not allow operator overloading.

In C++, methods and operators can be overloaded. This is static polymorphism.

Java, supports only single inheritance.
Effects of multiple inheritance can be achieved using the interfaces in Java.

C++ supports various types of inheritances including single and multiple inheritances.
Although there are problems arising from multiple inheritances, C++ uses the virtual keyword to resolve the problems.

Java has a built-in support for documentation comments (/**…*/). This way Java source files can have their own documentation.

C++ has no support for documentation comments.

Java is in-built thread support with a class “thread”. We can inherit the thread class and then override the run method.

C++ doesn’t have in-built thread support. It mostly relies on third-party threading libraries.

In C++, both the source code and filename do not have any relationship. This means that we can have many classes in the C++ program and the filename can be anything. It need not be the same as the class names.

In Java, there is a close relationship between the source code class and the filename. The class containing the source code and the filename should be the same.

For Example, if we have a class in Java named salary, then the filename which contains this class code should be “salary.java”.

 

Java doesn’t support the scope resolution operator. Java also doesn’t allow defining the functions outside. Everything related to the program including the main function needs to be inside a class.

C++ supports scope resolution operator as it uses it to access global variables. It also allows us to define functions outside the class and access them using the scope resolution operator.

 

No comments:

Post a Comment