Java is NOT my favourite programming language

I dislike Java very much, mainly due to its lack of features:

Object model

Objects and values

In C++, a primitive variable is an object, a class variable is an object, a pointer variable is an object, everything that has an address, except functions, is an object. An object can have reference semantics (by declaring references) or value semantics (the default behaviour). To demonstrate pass-by-reference, you can write a swap function template which swap 2 objects of the same type:

template <class T> void swap(T &a, T &b) {
  T &c = a;
  a = b;
  b = c;
}

In Java, a variable is NOT an object. Variable and objects are completely different things. You cannot access an object directly. You can only use them through handles (like using pointers in C++). A variable has only value semantics. There are no references in Java, therefore, you cannot write a function which swaps 2 integers. Also, as an object does not have value semantics, you cannot write generics which act identically on variables and objects.

Storage

In C++, you can choose to store objects on the heap, on the stack or in the program image. In Java, variables in functions can only be stored on the stack and objects can only be stored on the heap. Static variable is only possible in class scope.

Features

Multiple inheritance

Multiple inheritance is useful to the extent that:

  • Student is derived from Person
  • Composer is derived from Person
  • StudentComposer is derived form both Student and Composer

It is not possible in Java.

Operator overloading

If you write some mathematical classes, you can overload operators which enable you to do maths on it in C++, but, it is impossible to write a + b where one of them is user-defined type in Java.

Pointers

Pointers are undoubtly the most useful feature in C and C++, they allow programmers to do whatever foobar they want. You can write drivers, operating systems, etc in C++. For example, if the hardware clock of a particular system contains 8 bytes and is accessible via memory address 0x12345678 and its ABI specifies long to be 8 bytes, you can access the hardware clock by declaraing the following in C++:

volatile long &hwclock = *reinterpret_cast<volatile long *>(0x12345678);

hwclock now refers to the hardware clock. It is impossible to write such things in Java. Pointers are so powerful that you can do any levels of abstraction you like. In conclusion, many useful features are missing in Java, especially operator overloading that makes writing good libraries difficult.

Leave a Reply

Your email address will not be published. Required fields are marked *