If you want to get an idea of how references work during the creation of an object and when you pass a reference by parameter, I suggest taking a look at my answer to the question Pass-by Object Reference Wrapper to Method.
Basically doing this:
Integer k = new Integer(1);
You declare a variable and assign a new instance to it. A possible representation would be:
We can say that k
is a variable that references the object of type Integer
whose internal value is 1
.
So far nothing unlike a C pointer referencing a memory structure. However, the difference in Java is evident when you think about what you can do with this reference.
In C you are literally referencing the memory and can even perform operations with the pointer, such as p++
to move to the next position in memory. You can also access the bytes of that object by disregarding the content.
In Java, on the other hand, variables that reference objects are only means to access such objects and cannot be used as generic references to memory positions, nor to modify these objects directly.
Basically, all a reference can do is access members, attributes and methods of the objects in question.
Exact. Variables by reference only point to an address in memory. Roughly, they are aliases (shortcuts)
– Daniel Omine