With objects, such as
Strings, we can’t use the primitive equality operator
== to check for equality between two strings. To test equality with strings, we use a built-in method called
String flavor1 = "Mango"; String flavor2 = "Peach"; System.out.println(flavor1.equals("Mango")); // prints true System.out.println(flavor2.equals("Mango")); // prints false
Side note, there’s also an
equalsIgnoreCase() method that compares two strings without considering upper/lower cases.
Keep Reading: AP Computer Science A Students
We can also compare
String values lexicographically (think dictionary order) using the
.compareTo() method. When we call the
.compareTo() method, each character in the
String is converted to Unicode; then the Unicode character from each
String is compared.
The method will return an
int that represents the difference between the two Strings.
String flavor1 = "Mango"; String flavor2 = "Peach"; System.out.println(flavor1.compareTo(flavor2));
Our program above will output
When we use
.compareTo(), we must pay attention to the return value:
- If the method returns
0, the two
Strings are equal.
- If the value is less than
0, then the
Stringobject is lexicographically less than the
- If the value is greater than
0, then the
Stringobject is lexicographically greater than the
In the example above,
"Mango" comes before
"Peach", so we get a negative number (we specifically get
-3 because the Unicode values of
"P" differ by 3). If we did
flavor2.compareTo(flavor1), we would get
3, signifying that
"Peach" is greater than
Note: Make sure to pay attention to capitalization when using
.compareTo(). Upper case and lower case letters have different Unicode values. For example, when comparing
"Peach", we got
-3, meaning that
"Mango" was smaller. But if we compare
"Peach" we get
29. The Unicode value for lower case
"m" is actually larger than upper case
.compareToIgnoreCase() will perform the same task, but will not consider upper/lower case.
In Password.java, there’s a
password with a value of
equals() to print out whether it’s the same as another string.