How many objects are there in this program? How many reference variables are there?
There is one object (after the new
operator has worked)
and there are two reference variables.
Here is a picture showing the situation in the new program:
Now when the expression strA == strB
is evaluated,
it is true
because the contents of strA
and of strB
are the same
(ie. they both contain the same reference).
String strA; // will contain 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." ); |
When two reference variables refer to the same object,
the ==
operator will evaluate to true.