Java vs C++

Updated: 2021-11-27

Memory Management

  • Java uses virtual machines(JVM) which has built-in garbage collection
    • JVM will clean up the objects you created when they are no longer used.
    • you do not need to know where the objects are located in memory; and JVM may move the objects around due to garbage collection
  • C++ could directly work with memory locations
    • has to manually track the usage of the objects and free the memory
    • once an object is allocated in moemory it does not move. You can find the location using address-of operator &, like &x

Stack vs Heap

  • stack: stack pointer move down to create new memory, move up to release. efficient.
  • heap: unlike the stack, the compiler does not need to know how long the storage must stay on the heap. dynamically create and release. Takes longer time than stack.

Diff in Java and C++

  • Java:
    • primitive on stack: boolean, char, byte, short, int, long, float, double, void. more efficient
    • all objects are created on the heap, using new
    • object reference on the stack
    • A a; means a is just a reference, nothing is created yet; A a = new A(); will actually create an object in heap
    • both stack and heap in Java are managed by JVM, will be auto cleaned up.
  • C++:
    • A a; will allocate memory in stack; A* a = new A(); or auto a = std::make_unique<A>(); will allocate memory in heap, and a is a pointer to that memory.
    • can also be created in the heap using C apis like malloc()
    • stack memory will be auto-released as soon as the variable is out of scope, but not for heap (so if they are not freed there's a memory leak)

Objects

  • int i: i is not an object in Java, Integer i is
  • int i: i is an object in C++ (int itself is a type, which is not an object)

Early Binding vs Late Binding

  • Java: Early binding, know the type during compiling
  • C++: Late binding, do not know the exact type during compiling, but know in run time. Use virtual.

Destructor

  • Java: does not have destructors; the closest concept is the finalize() method, it is triggered during garbage collection, outside of program's flow of constrol, may or may not be called.
  • C++: destructors run within the program's flow of control, guaranteed to run.

Inheritance

  • Java: single rooted hierarchy, inherit from Object, does not support multiple inheritance
  • C++: multi-inherit

Pass By Value vs Pass By Reference

  • Java parameters (including object's reference) are passed by value
  • C++ parameters could be passed by value, pointer or reference.

Type

  • Java does not support unsigned arithmetic, C++ does.

Overloading

  • Java does not support operator overloading, only method overloading
  • C++ C++ supports both method overloading & operator overloading

Portability

  • C++: platform dependent, the source must be recompiled for different platforms
  • Java: platform independent, Java code will be compiled to bytecode, which will be used by JVMs. No need to recompile since JVM will handle differences among different platforms.

Functions

  • C++: Functions and data may exist external to any class, global and namespace scopes are available.
  • Java: All function and data exist within classes; package scope are available.

Return Code

C++

the integer returned from the main function is the process exit status, indicating if the code is successful

Java

Not the return code from main, instead System.exit(int status) has to be called.

public static void main(String[] args) {
  System.exit(1);
}

Actually, Java's main function has to return void, otherwise an error will be thrown:

Error: Main method must return a value of type void in class XXX, please define the main method as: public static void main(String[] args)

Interacting with native systems

  • C++: can direct call native system libraries.
  • Java: only through JNI (Java Native Interface) or JNA (Java Native Access) or the new Foreign-Memory Access API / Foreign Linker API

Filenames

  • Java: class name must match filename, e.g class A muast be stored in A.java
  • C++: no such restriction

C++ in JVM

From JDK 16, C++14 features can be used in JVM

Exceptions

Run-time checking: C++ exception specifications are checked at runtime rather than at compile time, so they offer no programmer guarantees that all exceptions have been handled.

Compare Strings

In C++, string::operator==() is using string::compare() under the hood, so the 2 strings can be compared directly using ==.

In Java, == checks if two strings are pointing to the same string, but to compare string content, use equlas().