Comparing Strings
A number of methods for string comparison are available in the Java String class. The following are a few of the most commonly utilised methods:
- == operator
- compareTo() method
- equals() method
- equalsIgnoreCase() method
- Objects.equals() method
Let's get into each method in detail.
== operator
The == operator checks for reference equality rather than value equality that is, whether they are the same object. The string comparison returns true if two String variables point to the same memory object. Otherwise, the string comparison returns false.
Take a look at the example below to see how the == operator functions.
Example
With the help of the following code snippet, we will see the working of == operator.
Code
class string_comparison
{
public static void main(String args[])
{
String s1="String";
String s2="String";
String s3=new String("String");
System.out.println(s1==s2);//returns true because both the objects refer to same instance
System.out.println(s1==s3);// returns false because s3 refers to nonpool instance )
}
}
You can also try this code with Online Java Compiler
Run Code
Output
true
false
You can also try this code with Online Java Compiler
Run Code
Explanation
The first statement returns true because the compiler has interned the literals, so they all refer to the same object.
The second statement returns false because each of the two string variables points to a different memory object.
Also see, Swap Function in Java
compareTo() method
When we need to identify the lexicographic order of Strings, we use the compareTo() method. It compares char values in the same way that equals() does.
The return value of the function is determined as :
- It will return a value of 0 if the two strings are identical.
- If the first String object comes after the second string (string1 > string2), it returns a positive integer.
- If the first String object predates the second string(string1 < string2), it returns a negative integer.
Syntax
The compareTo() method has the following syntax:
public int compareTo(String s)
You can also try this code with Online Java Compiler
Run Code
Example
With the help of the following code snippet, we will see the working of compareTo() method.
Code
class String_comparison{
public static void main(String args[]){
String s1="String";
String s2="String";
String s3="Comparison";
System.out.println(s1.compareTo(s2));//0
System.out.println(s1.compareTo(s3));//16(because s1>s3)
System.out.println(s3.compareTo(s1));//-16(because s3 < s1 )
}
}
You can also try this code with Online Java Compiler
Run Code
Output
0
16
-16
You can also try this code with Online Java Compiler
Run Code
Explanation
The first statement prints 0 because the two strings are identical.
The second statement returns a positive value as String s1 > String s3.
The third statement returns a negative value as String s3 < String s1.
You can also read about the topic of Java Destructor and Hashcode Method in Java.
equals() method
The equals() function in Java compares two provided strings depending on their data. It returns true if the contents of both strings are identical. It returns false if any character does not match.
Syntax
The equals() method has the following syntax:
public boolean equals(object a)
You can also try this code with Online Java Compiler
Run Code
Example
With the help of the following code snippet, we will see the working of equals() method.
Code
class String_comparison{
public static void main(String args[])
{
String s1="String";
String s2="String";
String s3="Comparison";
System.out.println(s1.equals(s2));//true
System.out.println(s1.equals(s3));//false because both are different strings
}
}
You can also try this code with Online Java Compiler
Run Code
Output
true
false
You can also try this code with Online Java Compiler
Run Code
Explanation
The first statement prints true because the two strings are identical.
The second statement prints false because both are different strings.
equalsIgnoreCase() method
The equalsIgnoreCase() compares two strings lexicographically, disregarding case differences. It returns true if the contents of both strings are identical regardless of the string's case (lower or upper); otherwise, it returns false.
Syntax
The equalsIgnoreCase() method has the following syntax:
public boolean equalsIgnoreCase(String s)
You can also try this code with Online Java Compiler
Run Code
Example
With the help of the following code snippet, we will see the working of equals() method.
Code
class String_comparison{
public static void main(String args[]){
String s1="String";
String s2="STRING";
String s3="Comparison";
System.out.println(s1.equals(s2));//true
System.out.println(s1.equals(s3));//false because both are different strings
}
}
You can also try this code with Online Java Compiler
Run Code
Output
true
false
You can also try this code with Online Java Compiler
Run Code
Explanation
The first statement prints true because the two strings are identical, ignoring the case.
The second statement prints false because both are different strings.
Objects.equals() method
If the arguments are equal, this method returns true; otherwise, it returns false. As a result, true is returned if both arguments are null, and false is returned if only one argument is null. Otherwise, the equals() method is used to determine equality.
Syntax
The Object.equals() method has the following syntax:
public static boolean equals(Object a , Object b)
You can also try this code with Online Java Compiler
Run Code
Example
With the help of the following code snippet, we will see the working of equals() method.
Code
import java.util.*;
class String_comparison{
public static void main(String args[]){
String s1="String";
String s2="String";
String s3="Comparison";
System.out.println(Objects.equals(s1, s2));//true
System.out.println(Objects.equals(null, null));//true
System.out.println(Objects.equals(s1, s3));//false because both are different strings
}
}
You can also try this code with Online Java Compiler
Run Code
Output
true
true
false
You can also try this code with Online Java Compiler
Run Code
Try it on java compiler.
Explanation
The first statement prints true because the two strings are identical.
The second statement prints true because both are null strings having no data (identical).
The third statement prints false because both are different strings.
Must Read Conditional Statements in Java
Frequently Asked Questions
Are Strings in Java mutable?
No, Strings in Java are immutable.
Which method can be used to compare strings with null values?
The Objects.equals() method can be used to compare strings with null values.
What is the equals() method?
The equals() method is used when two strings are compared; it returns true if they are equal and false if they are not.
Which method compares the string ignoring the case of the letters?
The equalsIgnoreCase() method compares the string ignoring the case of the letters.
Conclusion
In this article, we have extensively discussed the different methods used for String comparison, along with their syntax and examples.
We hope that this blog has helped you enhance your knowledge regarding String comparison, and if you would like to learn more, check out our articles on Java. You can refer to our guided paths on the Coding Ninjas Studio platform to learn more about DSA, DBMS, Competitive Programming, Python, Java, Javascript, etc. To practice and improve yourself in the interview, you can also check out Top 100 SQL problems, Interview experience, Coding interview questions, and the Ultimate guide path for interviews.
Also check out - String Interview Questions In Java
Recommended problems -
Do upvote our blog to help other ninjas grow.
Happy Coding!!