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 = "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.
The ==
operator can create some confusion when comparing String
objects. 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 String
s). 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
.
Because of potential problems like those described above, you should always use the equals()
method to compare the contents of two String
objects.
The equalsIgnoreCase()
method is very similar to the equals()
method. As its name implies, it ignores case when determining if two String
s are equivalent. This method is very useful when users type responses to prompts in your program. The equalsIgnoreCase()
method allow 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, it returns a negative value. If the two strings are equal, it returns zero. If the argument string is first, it returns a positive number.