go to previous page   go to home page   go to next page hear noise highlighting

In the above program, what is the object str ?

Answer:

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.


Larger Example

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.


QUESTION 8:

What happens when the statement

value = 32912;

is executed?


go to previous page   go to home page   go to next page