How would the program change if the second statement were changed to:
String str2 = "String Literal" ; // small difference
Now, since the second literal is not identical to the first, two
literal string objects are created, and
variables str1
and str2
refer to
different objects.
==
are always equal()
The ==
determines if two
names both refer to the same object.
It is common in the real world (and in programs) for an object to
have several names, for example "Mark Twain" and "Samuel Clemens"
are two names for the same author.
Consider two strings:
String strA = new String ("The Gingham Dog"); String strB = strA;
strA == strB
strA.equals( strB )
If ==
is true, then so does equals()
.
In the code that follows, will equals()
report true or
false?
String lit1 = "String Literal" ; String lit2 = "String Literal" ; if ( lit1.equals( lit2 ) ) System.out.println("TRUE"); else System.out.println("FALSE");