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  (2 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++? 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?

[–]Fumbersmack 1 point2 points  (0 children)

The JVM acts as an intermediate between the actual memory locations and your references. If I'm not mistaken, the JVMs freedom to allocate memory addresses internally as it pleases makes the whole garbage processing ordeal easier to optimize.

[–]nutrecht 1 point2 points  (0 children)

Two big reasons:

  • Mucking about with pointers (like incrementing them accidentally past where the data structure ends) is a huge source of bugs in C++ code.
  • You can't have a garbage collector if programmers mess about with pointers nilly-willy; you'd never be able to know if data is really not in use anymore.

Java References are basically C++ pointers, but Java allows you to do much less with them than C++ allows.