In the above program, what is the object str ?
Trick Question! There is no object str, only
a reference variable of that name.
However, people (and even books) often talk this way. Rather than saying:
"The object referred to by the variable str... "
they say
"The object referred to by the variable str... "
to mean the same thing. Most of the time the meaning is clear enough. BUT sometimes you need to think carefully about this.
Here is a slightly larger version of the example program, now with a new variable of a primitive type:
public class EgString2
{
public static void main ( String[] args )
{
String str;
long value;
str = new String( "The Gingham Dog" );
value = 32912;
System.out.println( str );
System.out.println( value );
}
}
We have seen that
str = new String( "The Gingham Dog" );
constructs a new object and puts a reference to it in str.
What happens when the statement
value = 32912;
is executed?