Object Reference

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   hear noise   go to next page

Answer:

An assignment statement asks for the computer to perform two steps, in order:

  1. Evaluate the expression on the right of the =
  2. 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 created

Now 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?