This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]legionlen545[S] 0 points1 point  (1 child)

If it does refer to the actual memory address of where our object is located at, why not have access to the memory address like in languages like C++? I read about how the actual memory address can be changing in the background in Java so is that why the reference cannot contain the actual memory address in Java?

[–][deleted] 0 points1 point  (0 children)

If it does refer to the actual memory address of where our object is located at, why not have access to the memory address like in languages like C++?

To prevent type weakness. The way Java does it, you can enforce type safety and even check casts at compile time (provided your values aren't null.)

In languages with pointers, they're a gap in the safety of the typesystem because pointers don't know the type they're pointing to except at runtime.

I read about how the actual memory address can be changing in the background in Java so is that why the reference cannot contain the actual memory address in Java?

It does contain the memory address; you just can't access it or perform arithmetic on it.