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:
public boolean equals(object1 object2)
javaLet’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:
public class Main {
public static void main(String[] args) {
String string1 = "This is a string";
String string2 = "This is a string";
String string3 = " This is a different string";
System.out.println(string1.equals(string2));
System.out.println(string1.equals(string3));
}
}
javaIf we use the Java command System.out.println
to print the output of this code, we get:
true
false
javastring1
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:
public int compareTo(string1 string2)
javaLet’s now look at a simple example. We’ll once again create three strings and compare the first with the other two:
public class Main {
public static void main(String[] args) {
String string1 = "This is a string";
String string2 = "This is a string";
String string3 = "This is a completely different string";
System.out.println(string1.compareTo(string2));
System.out.println(string1.compareTo(string3));
}
}
javaWhen output the code with System.out.println
, we get:
0
16
javaJava’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:
public class Main {
public static void main(String[] args) {
String string1 = "This is a string";
String string2 = "This is a string";
String string3 = "THIS IS A STRING";
System.out.println(string1.compareToIgnoreCase(string2));
System.out.println(string1.compareToIgnoreCase(string3));
}
}
javaThe output shows that the strings are identical:
0
0
javaHow 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:
public class Main {
public static void main(String[] args) {
String string1 = "This is a string";
String string2 = "This is a string";
String string3 = new String ("This is a string");
System.out.println(string1==string2);
System.out.println(string1==string3);
}
}
javaSince 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.
true
false
java