Wednesday, 28 January 2015

A Mini Comparison of C++ and Java.



C++
Java
Write once, compile anywhere.
  • Compile into native machine code to take advantage of platform-specific features. 
  • Need to be re-compiled for each target platform.

Write once, compile once - portability
  • Compiled into virtual machine byte code, to be run on Java Virtual Machine, which requires Java Runtime Environment.
  • Independent of operating system.

Manual memory management.        
  • Programmer controlled.
    • Low-level approach.
    • High-lever approach – Garbage Collection and RAII.

Automatic Garbage Collection.
  • Program automatically finds and reclaims memory that’s no longer required.
  • Eliminates bugs due to memory leaks and dangling pointers.
  • Overhead.
    • Run more slowly.
    • Over-allocate memory.
    • May not free memory at best possible time.
  • Mechanisms:
    • reference counting – track any variable is reference an object (won’t work for circular references).
    • mark and sweep – marking of objects that can be accessed, unmark objects that are de-allocated (will work for circular references), less efficient.

Multiple inheritances.
Single inheritance.
  •      Multiple inheritance can be simulated via interface.

Methods need to be explicitly declared virtual1.
Non-static2 Java methods are always virtual1.
Data type sizes is implementation-dependent.
Defined sizes for primitive data type.
Linked list needs to be implemented.
Linked list is implemented in standard library.
A string is character arrays.
A string is an object of String class.
  1. A virtual method is a method which its implementation is determined at run time, dependent on the actual type or class that is invoking the object.
  2. A static method in Java is a method that can be called without needing to instantiate an object of class. An example:  public static void main(String[] args)




No comments:

Post a Comment