Frequently asked questions
Q- I just can't wait any longer. What is a reference already?!
A- It literally is the same thing as a reference in normal (non-programming) language. It is a value that identifies/points to the 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.
Q- 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()
.
A- 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 will be covered in Chapter 12, The Stack, the Heap, and the Garbage Collector.