equals & compareTo methods
Objective #1: Compare strings correctly using the compareTo method.
- The compareTo method is used to alphabetically compare two strings. The expression
string1.compareTo(string2)
returns a positive number if string1 is alphabetically greater than string2. It returns the value zero if they are equal. It returns a
negative number if string1 is alphabetically less
than string2.
For example in the following code segment
String string1 = "aardvark";
String string2 = "zebra";
String string3 = "Carrot";
String string4 = "apple";
if (string1.compareTo(string2) < 0)
{
System.out.println("string1 aardvark is less than string2 zebra");
}
else if (string1.compareTo(string2) > 0)
{
System.out.println("string1 aardvark is greater than string2 zebra");
}
else if (string1.compareTo(string2) == 0)
{
System.out.println("string1 aardvark is the same as string2 zebra");
}
the output is "string1 aardvark is less than string2 zebra" since the ASCII/Unicode value for lowercase a is 97 and the ASCII/Unicode value for lowercase z is 122. The compareTo method usually returns
the difference between the ASCII value of the first character in the implicit parameter (the object typed before the dot operator) and the ASCII value of the first character of the explicit parameter
(i.e. the parameter in the parentheses). In this case 97 minus 122 is -25 which is less than zero.
Note that if string1 was equal to "Carrot" and string2 was equal to "apple" then "Carrot" would be considered less than apple since 67-97 is -30. Also, if string1 was equal to "apple" and string2 was
equal to "aardvark" then it comes down to the p in apple having an ASCII value of 112 minus the second a in aardvark with an ASCII value of 97. Since 112 minus 97 is 15 then apple is considered to be
greater than aardvark alphabetically. In other words, characters and letters are compared one by one until a tie is broken. Digits that are found in a string value such as the 2 in "R2D2" have ASCII
values as well with a 2's value being 50. See asciitable.com for a complete list of ASCII values.
Objective #2: Compare strings correctly using the equals method.
- When comparing String objects with an if statement, it is necessary to
use the equals method from the String class rather than the == symbol. The == only checks to see if the object reference (i.e. memory addresses) stored
by two object variables are equal to each other. The equals method compares
the two String objects themselves letter by letter.
String mySchool = "Wyomissing";
String yourSchool = "Wilson";
if (mySchool.equals(yourSchool))
{
System.out.println("You go to my school");
}
else
{
System.out.println("You do not go to my school");
}
The code segment above is the correct way to test the two strings. The following
code segment would not work correctly since the object references (i.e. memory
addresses) of mySchool and yourSchool are not going to be the same and therefore,
even though they store the same string literal "Wyomissing", the progam will
not print "You go to my school".
String mySchool = "Wyomissing";
String yourSchool = "Wyomissing";
if (mySchool == yourSchool)
{
System.out.println("You go to my school");
}
- The equals method
is available in practically any class that was fully developed. However,
if you are testing an object variable
to see if it stores the null value (i.e. memory address 0), then you must
use the ==.
String myName = null;
String yourName = "Betty";
String herName = "Sally";
if (myName == null)
{
System.out.println("myName is not yet instantiated");
}
else if (myName.equals(yourName))
{
System.out.println("myName is equal to yourName");
}