Answer:
An assignment statement asks for the computer to perform two steps, in order:
- Evaluate the expression on the right of the
= - Store the value in the variable on the left of the
=
Object Reference
Here is the assignment statement we are considering:
str = new String( "Elementary, my dear Watson!" );
It works like this:
1. Evaluate the expression.
The expression
new String( "Elementary, my dear Watson!" )is evaluated. This creates a new object. A reference to that object describes the object's location in memory. It enables the Java virtual computer to find the object.
2. Store the value in the variable.
In the second step, the reference is stored in the reference variable:
str = The reference to the string just createdNow whenever the program needs to refer to the object it uses the variable
str.
A reference is like a cell phone number. Someone who has your number can send you a message, and ask you to do something, no matter where you are. Think of your boss calling you and sending a message, "Get to work on that report!" This is like using an object reference to ask the object to run a method.
Sometimes the variable str is called the "name" of the object.
This is sloppy and can lead to confusion if you are not careful.
The object, the reference variable, and the reference are three different things.
QUESTION 6:
Are you and your cell phone number different things?