Compare Strings
To compare the equalities of strings, Java provides various methods to help us do so.
Using .equals()
.equals()
In Java, the .equals()
method is used to compare the contents (values) of two strings to determine if they are equal.
When you use .equals()
to compare strings, it checks whether the character sequences within the two string objects are identical.
This means that if the characters in both strings are the same and appear in the same order, the .equals()
method returns true
, indicating that the strings are equal; otherwise, it returns false
.
Using .comparesTo()
.comparesTo()
In Java, the .compareTo()
method is used to compare strings lexicographically (i.e., in dictionary order) to determine their relative ordering.
This method is available in the java.lang.String
class and returns an integer value that indicates the comparison result.
The .compareTo()
method has the following signature:
It takes another string (
anotherString
) as its argument.It returns an integer value, which can have one of three possible outcomes:
If the calling string (the one on which
.compareTo()
is invoked) is lexicographically less than theanotherString
, it returns a negative integer.If the calling string is lexicographically greater than the
anotherString
, it returns a positive integer.If the calling string and
anotherString
are lexicographically equal, it returns0
.
Lexicographic Ordering:
"apple" comes before "banana" because 'a' comes before 'b' in the Unicode character set.
"apple" comes after "app" because 'l' comes after 'p,' and the length of "apple" is greater than "app."
"Apple" comes before "apple" because 'A' comes before 'a' in the Unicode character set.
"apple" comes before "Applesauce" because "apple" is shorter and is a prefix of "Applesauce."
Last updated