String Comparison in Java: A String in Java is a sequence of characters. It’s important to know that strings cannot be changed once created. In Java, strings are often used, and comparing strings is a common task. You might need to compare strings in Java in many situations, like alphabetically organizing a list of words or checking if a certain word is already in a database.
In this blog post, we will explore different ways to compare strings efficiently and quickly in Java. We will detail each method and provide practical examples to help you understand them better. So, let’s dive right in and get started!
String Comparison In Java
We can compare two string objects in 3 ways in Java programming language. That is:
- By Using == operator
- By using equals() method
- By Using compareTo() method
By Using == operator
If you want to compare the reference of the string objects but not the string object content, then, in that case, you can use the == operator. After comparing if both the operands have the same reference, then, in that case, it will return true; otherwise, it will return false.
Program: How do you compare Strings using the == operator?
public class String_Comparison_Using_Operator { public static void main(String[] args) { String str1="Software Testingo"; String str2="Software Testingo"; String str3 = new String("SoftwareTestingo"); // String Comparison between Str1 and Str2 if (str1==str2) { System.out.println("Str1 & Str2 Reference Is Same"); } else { System.out.println("Reference Of Both the String Object are Not Same"); } //String Comparison between Str1 and Str3 if (str1==str3) { System.out.println("Str1 & Str3 Reference Is Same"); } else { System.out.println("Reference Of Both the String Object are Not Same"); } } }
Output:
Str1 & Str2 Reference Is Same Reference Of Both the String Object are Not Same
By equals() method
To compare the contents of two string objects, you can use the equals() method. equals() is an object class method. Still, the string class overrides the equals() method and provides the below format of equals() methods
- public boolean equals(Object obj): Compares the string with the specified object. Returns true if the actual content is equal; otherwise, returns false.
- Public Boolean equalsIgnoreCase(String str): Compares the string’s actual content with the specified string’s content. Returns true if two strings’ content is equal, ignoring case; otherwise, returns false.
Program: How do you compare the Strings content in Java?
package com.SoftwareTestingO.Java.basics; public class String_Comparison_Using_Equal_Method { public static void main(String[] args) { String str1="Software Testingo"; String str2="Software Testingo"; String str3 = new String("Software Testingo"); String str4="Software"; String str5="SOFTWARE TESTINGO"; //Comparing Str1 & Str2 if(str1.equals(str2)) { System.out.println("Both Str1 & Str2 have the same value"); } else { System.out.println("Both have the different value"); } //Comparing Str1 & Str3 if(str1.equals(str3)) { System.out.println("Both Str1 & Str3 have the same value"); } else { System.out.println("Both have the different value"); } //Comparing Str2 & Str4 if(str2.equals(str4)) { System.out.println("Both Str2 & Str4 have the same value"); } else { System.out.println("Both str2 & str4 have the different value"); } //Comparing Str2 & Str5, Here content same but different in case if(str2.equals(str5)) { System.out.println("Both Str2 & Str5 have the same value"); } else { System.out.println("Both Str2 & Str5 have the same value but different in case"); } //Comparing Str2 & Str5, Here content same but ignore case if(str2.equalsIgnoreCase(str5)) { System.out.println("Both Str2 & Str5 have the same value"); } else { System.out.println("Both have the same value but different in case"); } } }
Output:
Both Str1 & Str2 have the same value Both Str1 & Str3 have the same value Both str2 & str4 have the different value Both Str2 & Str5 have the same value but different in case Both Str2 & Str5 have the same value
By compareTo()
compareTo() method compares the two strings lexicographically, i.e., character by character. The string class provides the following formats of the compareTo() method.
- public int compareTo(String str)
- public int compareToIgnoreCase(String str)
It returns 0 if the argument string is equal to this string, less than 0 if it is lexicographically less than the string argument, and greater than 0 if it is lexicographically greater than the string argument.
Program: Write a Java Program to compare Two Strings using the compareTo() method.
package com.SoftwareTestingO.Java.basics; class string_comparison { String str1="Software Testingo"; String str2="Software Testingo"; String str3="Software"; String str4="Testingo"; public void stringcompare() { //return 0, because content are same. System.out.println(str1.compareTo(str2)); //return greater than 0, because str1 //lexicographically greater than str2. System.out.println(str1.compareTo(str3)); //return less than 0, because str1 //lexicographically less than str2. System.out.println(str3.compareTo(str4)); } } public class String_Comparison_Using_CompareTo { public static void main(String[] args) { string_comparison obj=new string_comparison(); obj.stringcompare(); } }
Output:
0 9 -1
Conclusion:
In this guide, you learned the different methods for comparing strings in Java. You saw different ways to compare strings, each with its specific use. However, the methods Objects.equals() and String.equals() are versatile and can be used in many situations to get the best results for your Java program.