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

How many objects are there in this program? How many reference variables are there?

Answer:

There is one object (after the new operator has worked) and there are two reference variables.


Picture of One Object and Two Reference Variables

One Object, two reference variables

Now when the expression strA == strB is evaluated, it is true because the contents of strA and of strB are the same (they both contain the same reference).

When two reference variables refer to the same object, the == operator evaluates to true.



    String strA;  // contains the reference to the object
    String strB;  // another copy of the reference to the object
     
    strA = new String( "The Gingham Dog" ); 
    System.out.println( strA   ); 

    strB = strA;
    System.out.println( strB   );

    if ( strA == strB )
      System.out.println( "Same info in each reference variable." );  

QUESTION 18:

In this program, does the == operator look at objects?