Java Reference
In-Depth Information
Comparing String s with the == Operator
The condition at line 23 uses the equality operator == to compare String s1 for equality
with the String literal "hello" . When primitive-type values are compared with == , the
result is true if both values are identical . When references are compared with == , the result
is true if both references refer to the same object in memory . To compare the actual contents
(or state information) of objects for equality, a method must be invoked. In the case of
String s, that method is equals . The preceding condition evaluates to false at line 23 be-
cause the reference s1 was initialized with the statement
s1 = new String( "hello" );
which creates a new String object with a copy of string literal "hello" and assigns the new
object to variable s1 . If s1 had been initialized with the statement
s1 = "hello" ;
which directly assigns the string literal "hello" to variable s1 , the condition would be
true . Remember that Java treats all string literal objects with the same contents as one
String object to which there can be many references. Thus, lines 8, 17 and 23 all refer to
the same String object "hello" in memory.
Common Programming Error 14.1
Comparing references with == can lead to logic errors, because == compares the references
to determine whether they refer to the same object, not whether two objects have the
same contents . When two separate objects that contain the same values are compared
with == , the result will be false . When comparing objects to determine whether they have
the same contents, use method equals .
String Method equalsIgnoreCase
If you're sorting String s, you may compare them for equality with method equals-
IgnoreCase , which ignores whether the letters in each String are uppercase or lowercase
when performing the comparison. Thus, "hello" and "HELLO" compare as equal. Line 29
uses String method equalsIgnoreCase to compare String s3 Happy Birthday —for
equality with String s4 happy birthday . The result of this comparison is true because
the comparison ignores case.
String Method compareTo
Lines 35-44 use method compareTo to compare String s. Method compareTo is declared
in the Comparable interface and implemented in the String class. Line 36 compares
String s1 to String s2 . Method compareTo returns 0 if the String s are equal, a negative
number if the String that invokes compareTo is less than the String that's passed as an
argument and a positive number if the String that invokes compareTo is greater than the
String that's passed as an argument. Method compareTo uses a lexicographical compari-
son—it compares the numeric values of corresponding characters in each String .
String Method regionMatches
The condition at line 47 uses a version of String method regionMatches to compare por-
tions of two String s for equality. The first argument to this version of the method is the
starting index in the String that invokes the method. The second argument is a compar-
 
Search WWH ::




Custom Search