Comparison of Java with C and C++

Java language has borrowed the syntax of C and C++ languages. Having are lot of similarities Java is very much different from C and C++. We have summarized the main differences here:

Data Type #

  • In Java, int takes four bytes as compared to C, C++ where it takes two bytes of memory.
  • In Java char takes two bytes as compared to C, C++ where it takes one byte of memory.
  • In Java new data type boolean is added, it is not available in C.
  • Java does not support typedef statements.

Operators #

  • Java adds a new operator in bitwise that is right shift operator’>>>’.
  • The “%” operator may be applied in Java for dividing float values whereas in C and C++ it is applicable to only integer operands.
  • Java does not support the special operator sizeof(), which is frequently used in C, and C++ to find the memory size of data elements.

Keywords #

  • Java introduced some new keywords such as super, try, catch, final, finally, synchronized, package, interface, etc.
  • Java dropped some keywords that were used in C and C++ such as typedef, delete, etc.

Loops #

  • Java supports a special type of for loop used with arrays. All other control statements are the same as in C, and C++.

Break and Continue #

  • In addition to the normal me of break and continue statements, Java support labeled break and continue statements to provide better control over loops.

Pointers #

  • Java does not support the concept of pointers. This is a very important concept in C and C++.

Preprocessor directives and header files: #

  • Java does not have preprocessor directives and header files.
  • Java uses import keyword to employ inbuilt in the Java class library.
  • Java uses the concept of packages to organize classes.

Portability #

  • Java programs are compiled to intermediate code i.e. Bytecode which is interpreted by the Java run time environment. The byte code is machine independent and can be executed on any platform.
  • In C and C++ the source code is compiled to machine-dependent executable code. That is executed on the actual machine and is not portable.

Inheritance #

  • The concept of inheritance is not available in C.
  • Java supports inheritance but does not support multiple inheritance directly which is available in C++.

Operator overloading #

  • The concept of operator overloading is not supported in C.
  • Java does not support operator overloading which is an important feature of C++.

String representation #

  • In C and C++ string is represented as a null-terminated array of characters.
  • In Java, every string is an object of the String class.

Memory management #

  • In C and C++ memory management is the programmer’s responsibility.
  • In Java programmer uses memory freely without worrying about deallocation.
  • In C free() function and in C++ delete() is used to deallocate memory.
  • In Java memory is deallocated using a garbage collection routine.

Multi-threading #

  • The concept of multi-threaded programming is used in Java and not in C and C++.

Exception handling #

  • The concept of exception handling is used in Java and C++ but not in C.
  • Java provides a better way of exception handling as compared to C++.

Console I/O #

  • The method of console input/output is entirely different in Java as compared to C and C++.

Coding Scheme #

  • C and C++ languages use ASCII code to represent characters but Java uses UNICODE code to represent characters.

Leave a Reply