Differences between C++ and Java

Have you been learning C++ from this website and are thinking about learning Java. This will show you the differences between C++ and Java. If you understand C++ well, you will have no problems learning Java quickly.

Everything is a class: In Java, everything is contained by classes, so there is no global data and no individual functions.

Inheritance in Java: Java makes much more use of inheritance than it does with C++. Applying Inheritance in C++ can get complex and confusing. Since Java is a more readable language, it doesn’t seem to have this problem.

Garbage collection: Java automates the recovery of dynamically created data. This is a major issue with programs that must run reliably for any period of time.

No Pointers: Pointers can be troublesome and can cause a significant amount of run time errors in C++. The advantage with Java is that they have eliminated pointers completely for primitive data types, by allowing just pass by value. This means the destination routine gets a copy of the data and cannot affect the original. An example of this


int incInt(int j)

{

++j;          // This will not change the original value of j

return(j); // Return increment version of j

}

More differences between Java and C++: 

  • No #: there is no text preprocessor so none of the # commands are available, for example #include, #define or #ifdef
  • Fixed number of parameters: Java does not allow functions with a varialbe number of parameters such as printf()
  • Array bounds checking: Not part of C++ but always applied in Java
  • No structs or unions are avaliable in Java
  • No operator overloading is possible in Java, for example the + operator cannot be given any changed functionality
  • Virtual: There is no need to use the virtual keyword in Java because all methods can be dynamically bound. In Java, the keyword “final” can be used to force static binding
  • No Templates exist in Java
  • No namespace is needed in Java, because everything is held inside a class, and classes are grouped together into packages

If you are interested in learning Java, a great website I would recommend is How to Program with Java, run by Trevor Page. His products and tutorials are awesome and are all explained in plain English.

Share this post