There are two objects, each with a unique reference. There are two reference variables, each containing a different reference.
== operator looks at the contents
of two reference variables.
If both reference variables contain the same reference,
then the result
Otherwise the result is false.
Since object references are unique,
operator returns true if two reference variables refer to the same object.
== operator does NOT look at objects!
It only looks at references (information about where an object is located).
Here is a section from the previous program, with an additional
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.");
Since the reference in
is different than the reference in
is false. (Look at the picture on the previous page.)
println() statement will not execute.
== operator look at the contents of the
objects when the