Javas "reference system" is its pointer system. They're basically pointers without the possibility of casting to an incompatible type. It also does not allow you to use pointer arithmetic and like C and C++ do. In C++ you can corrupt your data by:
class SomeClass {
private:
int a;
public:
SomeClass(int a): a(a) {}
};
Later you can corrupt an object of the class by basically doing this:
SomeClass victim(2);
unsigned char *hazardousPointer = (unsigned char *) &victim;
for (int i = 0; i < sizeof(SomeClass); i++)
hazardousPointer[i] = 0;
This piece of code just allowed us to violate the private
access rights and allowed to change its state (it could be also const
, that does not matter). This happened due to the fact that C/C++ pointers are just memory addresses which pretty much behave like integers with some restrictions. Furthermore hazardousPointer[i]
is just C/C++ syntactic suggar for *(hazardousPointer + i)
which is not the case with Java, where arrays are objects which can even be returned from methods and have their own methods.
Furthermore Java has a garbage collector which cares about memory leaks, where C++ relies on its system stack and wrappers or smart pointers.