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

Answer:

    str = new String( "You know my methods, Watson." );

Objects are Created at Run Time

public class StringDemo1
{
  public static void main ( String[] args )
  {
    String str;

    str = new String( "Elementary, my dear Watson!" );

  }
}

Before the program runs, there is no object. The new String object is created as the program runs.

The declaration

String str; 

creates a reference variable, but does not create a String object. The variable str is used to refer to a String after one has been created.

The next statement

str = new String( "Elementary, my dear Watson!" ); 

creates an object and puts a reference to that object in str.

After the program stops running, the String object no longer exists. Its memory is reclaimed by the computer system for other uses.


QUESTION 6:

(Review: ) What are the two steps in an assignment statement?


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