Web Developer said:
Wait a minute, in Java, an object reference also hold the memory address of
an object. Can you understand my confusion? I think my background Java may
curse me in C++.
It isn't clear what is meant by "the memory address of an object." Since an
object is a conceptual fiction and consists in reality of various control
structures, arrays, member data, etc. it is unclear as to just what exactly
a reference variable points to. Probably most implementation use it to
address the start of the structure containing all the object information,
but I don't know that structure myself.
I also believe that a reference in C++ is not necessarily an address of the
data object, but could be implemented in some other fashion (say, a handle
that refers to an address of the data object). The only requirement is that
the identifier used for the reference can be used interchangeably for the
declaration name of the object being referenced. Probably most
implementations simply use the address of the data object. The restrictions
on the C++ reference prohibit using it like a pointer, which does contain
the address of the data object pointed to.
Like a pointer, the Java reference variable contains an address, but the
language syntax checks the type of the primitive or object being stored into
it for compatibility. So, a Java reference cannot be used as a pointer; same
as in C++. In Java a reference variable can be set to null.
Since I am here to learn C++: can a reference in C++ be set to null? I
should think not, since it only stands for one object and cannot be changed.
Yes, these languages are different. It's a pity (in a way) that they use the
terms "reference" and "object" so differently.