Object reference vs pointer
DifferencesPointer
- You think of pointer as just another type like int, char, float, it takes up a constant amount of spaces in memory, and you can assign values to them.
- The value you assign to them is a memory
address, you can point them to a memory address. The type of the pointer tell it how to interpret the memory address that it is assigned.
You can interpret the memory address pointed by the pointer using the dereferencing operator (*).
AnA objectpointer can be assigned to point a NULL value
A pointer can be changed to point to any variable of the same type.
You can do pointer arithmetic, but you cannot with references.
References
A reference
onmust be initialized when it is declared
It cannot be NULL
You can use it by simply using the
othername. hand, first of all, doesn't exist in C, but in C++.
An object referenceReference is implemented via pointer,
but a constant pointer with automatic indirection, the compiler will apply the dereferencing for you automatically.
An object reference cannot be null, while a pointer can be pointed to NULL.
Think of
object references as an alias to
the existing object in memory.
Another variable name if you will.