The following methods should be used when comparing String objects:
Comparison Method |
Sample Syntax |
boolean equals(String anotherString); |
String aName = "Mat";
String anotherName = "Mat";
if (aName.equals(anotherName))
System.out.println("the same"); |
boolean equalsIgnoreCase(String anotherString); |
String aName = "Mat";
if (aName.equalsIgnoreCase("MAT"))
System.out.println("the same"); |
int compareTo(String anotherString) |
String aName = "Mat"
n = aName.compareTo("Rob"); // n < 0
n = aName.compareTo("Mat"); // n == 0
n = aName.compareTo("Amy"); // n > 0 |
|
The equals()
method evaluates the contents of two String
objects to determine if they are equivalent. The method returns true if the objects have identical contents. For example, the code below shows two String
objects and several comparisons. Each of the comparisons evaluate to true
; each comparison results in printing the line "Name's the same"
.
String aName = "Mat";
String anotherName = new String("Mat");
if (aName.equals(anotherName))
System.out.println("Name's the same");
if (anotherName.equals(aName))
System.out.println("Name's the same");
if (aName.equals("Mat"))
System.out.println("Name's the same");
Each String
shown above, aName
and anotherName
, is an object of type String
, so each String
has access to the equals()
method. The aName
object can call equals()
with aName.equals(anotherName)
, or the anotherName
object can call equals()
with anotherName.equals(aName)
. The equals()
method can take either a variable String
object or a literal String
as its argument.
In all three examples above, the boolean expression evaluates to true
.
The == operator can create some confusion when comparing objects. The == operator will check the reference value, or address, of where the object is being stored. It will not compare the data members of the objects. Because Strings
are objects and not primitive data types, Strings
cannot be compared with the == operator. However, due to the shortcuts that make String act in a similar way to primitive types, two Strings
created without the new
operator but with the same String
literal will actually point to the same address in memory. Observe the following code segment and its output:
String aGreeting1 = new String("Hello");
String anotherGreeting1 = new String("Hello");
if (aGreeting1 == anotherGreeting1)
System.out.println("This better not work!");
else
System.out.println("This prints since each object " +
"reference is different.");
String aGreeting2 = "Hello";
String anotherGreeting2 = "Hello";
if (aGreeting2 == anotherGreeting2)
System.out.println("This prints since both " +
"object references are the same!");
else
System.out.println("This does not print.");
Run Output:
This prints since each object reference is different.
This prints since both object references are the same!
The objects aGreeting1
and anotherGreeting1
are each instantiated using the new
command, which assigns a different reference to each object. The == operator compares the reference to each object, not their contents. Therefore the comparison (aGreeting1 == anotherGreeting1)
returns false
since the references are different.
The objects aGreeting2
and anotherGreeting2
are String
literals (created without the new
command - i.e. using the short-cut instantiation process unique to Strings). In this case, Java recognizes that the contents of the objects are the same and it creates only one instance, with aGreeting2
and anotherGreeting2
each referencing that instance. Since their references are the same, (aGreeting2 == anotherGreeting2)
returns true
.
-
When comparing objects to see if they are equal, always use the equals
method. It would be a rare occasion to care if they are occupying the same memory location. Remember that a String
is an object!
-
The equalsIgnoreCase()
method is very similar to the equals()
method. As its name implies, it ignores case when determining if two Strings
are equivalent. This method is very useful when users type responses to prompts in your program. The equalsIgnoreCase()
method allows you to test entered data without regard to capitalization.
- The
compareTo()
method compares the calling String
object and the String
argument to see which comes first in the lexicographic ordering. Lexicographic ordering is the same as alphabetical ordering when both strings are either all uppercase or all lowercase. If the calling string is first lexicographically, it returns a negative value. If the two strings are equal, it returns zero. If the argument string comes first lexicographically, it returns a positive number.
String bob = "Bob";
String bob2 = "bob";
String steve = "Steve";
System.out.println(bob.compareTo(bob2));
System.out.println(bob2.compareTo(bob));
System.out.println(steve.compareTo(bob2));
System.out.println(bob.compareTo(steve));
The output for this block of code would be:
-32
32
-15
-17