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

Answer:

There are two objects, each with its own reference. There are two reference variables, each containing a different reference.


Equality of References

    String strA;  // reference to the first object
    String strB;  // reference to the second object
     
    strA   = new String( "The Gingham Dog" );    // create the first object and  
                                                 // save its reference
    System.out.println( strA ); 

    strB   = new String( "The Calico Cat" );     // create the second object and
                                                 // save its reference
    System.out.println( strB );

    if ( strA == strB ) 
      System.out.println( "This will not print." );

The == operator looks at the contents of two reference variables. If both reference variables contain the same reference, then the result is true. Otherwise the result is false.

Since object references are unique, the == operator returns true if two reference variables refer to the same object.

The == operator does NOT look at objects!   It only looks at references.

Since the reference in strA is different than the reference in strB,

strA == strB

is false. (Look at the picture on the previous page.) The third println() statement will not execute.


QUESTION 16:

Did the == operator look at the objects when the if statement executed?