How Do I Compare Strings in Java?
Learn more about comparing Strings in Java using the equals to (=) operator.
Join the DZone community and get the full member experience.
Join For FreeIn this article, you are going to learn how to compare strings and the problems that occur when you compare string using equals to
(=
)operator.
You may also like: The Do's and Don'ts of Java Strings
Introduction
The String
is a special class in Java. We use String regularly in Java programs, so comparing two strings is a common practice in Java. In this article, I tried to answer the most common questions about the string, like: "How do I compare strings in Java?"
Comparing strings is very helpful during processes like authentication, sorting, reference matching, etc.
I have listed three different ways to compare strings in Java.
Using
equals()
method (comparing the content)Using
==
operator (comparing the object reference)Using
compareTo()
method (comparing strings lexicographically)
1. Compare Strings Using the Equals()
Method
In this way, I am using .equals()
instance method of the String class. Originally .equals()
method is the Object
class method, String class overrides it.
equals()
method compare two strings for value equality, whether they are logically equal.
equals()
method in String class takes another string as a parameter and compares it with the specified string. It returns true
if — and only if — the parameter string is not null and contains the same characters as the specified string.
public boolean equals(Object anObject)
It compare this string with the argument strings and return true if the argument is not null and contains the same character as the specified string.
param -
another string
returns -
true - if argument is not null and it contains same characters as the specified string
false - if the argument is null or it does not contain same characters as the specified string
ex. firstString.equals(secondString)
// returns true if and only if the secondString is not null and contains the same characters as firstString.
I have asked the program to compare strings using the equals()
method below:
xxxxxxxxxx
/**
* A Java program to compare two strings using equsls()
* and equalsIgnoreCase() method of the String.
*
* @author Gaurav Kukade at coderolls.com
*/
public class CompareUsingEquals {
public static void main(String[] args) {
String firstString = "coderolls";
String secondString = "javablog";
String thirdString = "coderolls";
String fourthString = "CodeRolls";
System.out.println("Comparing strings using equals() and equalsIgnoreCase() method\n");
// Using equals() method
System.out.print("firstString.equals(secondString) : ");
System.out.println(firstString.equals(secondString));
System.out.print("firstString.equals(thirdString) : ");
System.out.println(firstString.equals(thirdString));
/*
* Using equalsIgnoreCase() method to ignore
* case consideration (i.e. Capital or small) of both the strings.
*/
System.out.print("firstString.equalsIgnoreCase(fourthString) : ");
System.out.println(firstString.equalsIgnoreCase(fourthString));
}
}
Output:
xxxxxxxxxx
Comparing strings using equals() and equalsIgnoreCase() method
firstString.equals(secondString) : false
firstString.equals(thirdString) : true
firstString.equalsIgnoreCase(fourthString) : true
2. Compare Strings Using ==
Operator
In String, the ==
operator is used to comparing the reference of the given strings, depending on if they are referring to the same objects.
When you compare two strings using ==
operator, it will return true
if the string variables are pointing toward the same java object. Otherwise, it will return false
.
I have given a Java program to compare using ==
operator below:
/**
* A Java program to compare strings using == operator.
*
* == operator ckecks whether both the strings referring
* to the same String Object.
*
* @author Gaurav Kukade at coderolls.com
*/
public class CompareUsingEqualsToOperator {
public static void main(String[] args) {
String firstString = "coderolls";
String secondString = "javablog";
String thirdString = "coderolls";
// creating new String object with the same value as firstString or thirdString
String fourthString = new String("coderolls");
System.out.println("Comparing strings using == operator \n");
System.out.print("firstString == secondString : ");
System.out.println(firstString == secondString);
/*
* Here firstString and thirdString is referring to the same String object
* hence it will print 'true'.
*/
System.out.print("firstString == thirdString : ");
System.out.println(firstString == thirdString);
/*
* Here firstString and fourthString have same value
* but they are referring to the different String object.
*
* hence it will print 'false'
*/
System.out.print("firstString == fourthString : ");
System.out.println(firstString == fourthString);
}
}
Output:
xxxxxxxxxx
Comparing strings using == operator
firstString == secondString : false
firstString == thirdString : true
firstString == fourthString : false
Problems Using the ==
Operator for String Comparison
Most of the beginner Java developers commit this mistake by comparing two strings using the ==
operator.
Logically, they have to check whether both the string contains the same character sequence or not.
In Java Strings, the ==
operator is used to check the reference of both the string objects and equals()
method used to check the value equality of both strings.
==
– checks reference equality
equals()
– checks the value equality
When we assign a string value to the string variable, the JVM will check if the string with the equal value already present in the string pool or not. If it is not present in the string pool, it will be added to the constant pool and the reference to that string object is returned.
If it is present in the string pool, the reference to the memory address of that string object is returned.
The following image shows the pictorial explanation of the same.
firstString
" pointing towards the “coderolls” string in the String pool.
If we are assigning the equal value to another string variable, the JVM checks if the string with that value is present in the string constant pool or not.
Since the string object with that value is already created in the previous step, another string variable starts referring to the previously created string object instance.
The following image shows the pictorial explanation for the ‘firstString
’ and ‘secondString
’ pointing towards the “coderolls” string in the string pool.
When we create a string using the
new
operator, a new string object is created and stored in the Java heap space.
firstString
’ and ‘ secondString
’ pointing towards the “ coderolls
” string in the string pool and ‘ thirdString
’ pointing towards the “ coderolls
” in the Java heap space.
Output:
xxxxxxxxxx
Comparing strings using compareTo() and compareToIgnoreCase() method
firstString.compareTo(secondString) : 7
firstString.compareTo(thirdString) : -9
secondString.compareToIgnoreCase(fourthString) : 0
I have written a detailed article on how to compare strings lexicographically in Java if you want to learn more.
Conclusion
We can compare strings using the ways given below:
- Using
equals()
method:equals()
method in the strings used to check the string value equality whether they contain the same character sequence. - Using
==
operator:==
operator used to check the reference equality of the two strings, whether they are pointing towards the same string object. - Using
compareTo()
method:compareTo()
method used to check the strings lexicographically, i.e. alphabetically. Check the detailed articles on how to compare strings lexicographically.
Most of the beginner Java developers make mistakes while comparing strings. They want to check the content of the string, but they use the ==
operator to check it.
It is always advised to use equals()
method to compare the string based on its content.
If you have any queries about the code blocks given above, please write it down in the comment section below. Also, let me know if you have any other way to compare two strings in Java in the comment section.
This article originally published at coderolls.com/compare-strings-in-java.
Further Reading
The Do's and Don'ts of Java Strings
Published at DZone with permission of Gaurav Kukade. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments