Frequently asked questions
- I just can't wait any longer. What is a reference already!?
It literally is the same thing as a reference in normal (non-programming) language. It is a value that identifies/points to data rather than the actual data itself. One way of thinking about it is that a reference is a memory location/address. It identifies and gives access to the actual data at that location/address in memory.
- If it is not the actual object but just a reference, how come we can do things like call methods on it, such as
mySoldier.shootEnemy()
?Java works out the exact details under the hood, but you can think of a reference as being the controller of an object, and anything you want to do to the object you must do through the controller, because the actual object/memory itself cannot be directly accessed. More on this in Chapter 12, The Stack, the Heap, and the Garbage Collector.