How to compare strings in Java
There are three practical options for comparing strings in Java. Java’s equals()
method, the ==
operator and the Java method compareTo()
are all solid choices.
What are the options for comparing strings in Java?
While the primitive data types can easily be compared with each other in Java using the Java operator ==
, it’s a bit more complicated for strings. You can use the ==
operator with strings, but it will only check if they refer to the same object, not whether their content is the same. In some cases that might suffice or even be what you’re looking for, but in other cases you’ll want a way to check whether strings contain the same text. Luckily there are several options in this programming language for getting the result you’re looking for. They include:
- Java
equals()
- Java
compareTo()
for strings - Java Operator
==
- 99.9% uptime
- PHP 8.3 with JIT compiler
- SSL, DDoS protection, and backups
How to use Java equals()
The simplest and most practical option for comparing the content of two strings is the equals()
method. It compares the characters in the two strings. If they match exactly, the method returns true
. If they differ in any way, it returns false
. Here’s the syntax for the method:
Let’s look at a simple example. We’ll create three strings and compare the first of these strings with the other two. In practice, it will look as follows:
If we use the Java command System.out.println
to print the output of this code, we get:
string1
and string2
are identical, whereas string3
is different from the other two strings. While in this example we can easily recognize this with our eyes, there are other strings that might look the same at first glance but differ with regard to some small detail. Java’s equals()
is a great way to determine whether they are in fact the same.
How to use Java’s compareTo()
The second method is a little bit more complicated, but also reliable. Java’s compareTo()
method compares strings lexicographically based on the Unicode value of the individual characters in the strings. Each of the individual characters is checked against the others. If the strings match, the method will return 0. If the first string is shorter than the second, it will return a negative value. If it’s longer, it will return a positive value. Here’s the syntax for the method:
Let’s now look at a simple example. We’ll once again create three strings and compare the first with the other two:
When output the code with System.out.println
, we get:
Java’s compareTo() is case sensitive. If you want to compare strings without regard for lowercase and uppercase letters, use the modified method compareToIgnoreCase()
. It works as follows:
The output shows that the strings are identical:
How to use ==
to compare strings in Java
As mentioned above, you can use the ==
operator to compare two strings with each other. However, it will compare the references of the strings and not the actual values. Let’s see how this works with an example:
Since string1
and string2
refer to the same instance, the output will be true
when they are compared. But string3
refers to another instance. This means that the output will be false
, even though the text within the two strings is identical.