Before moving further please read the article String in Java. As we know String is the most common and widely used data type. There may be a lot of scenarios where we want to make decisions based on String comparison in java. Java provides different ways to compare two strings in java. In this post, we will see how java string compare works. We can compare string in java by the use of various methods.
1. String.equals() method in Java
2. String. equalsIgnoreCase() method in Java
3. Object.equals(object1, object2) method in Java
4. String.compareTo() method in Java
5. == operator in java
1. String equals() method in Java
In java, the equals() method is defined in the Object class and the String class is overriding the equals() method. In String class equals() method is used for String comparison in Java. We can compare two strings by using of equals() method. It compares the values of string for equality. This method compares the original content of the string and returns the value of the boolean. If any character is not matched, it returns false. If all characters are matched, it returns true. The String equals() method overrides the equals() method of the Object class.
firstString.equals(secondString);
If both strings are equals it will return true otherwise false. It will compare the original content of both strings.
class ExampleOfEquals { public static void main(String args[]) { String name = "Ravi"; String name1 = "Ravi"; System.out.println("Is both strings are equals = "+ name.equals(name1)); String address = new String("CHD"); String address1 = new String("CHD"); System.out.println("Is both strings are equals = "+ address.equals(address1)); String className = "MCA"; String className1 = new String("MCA"); System.out.println("Is both strings are equals ="+className.equals(className1)); String country = "India"; String country1 = "USA"; System.out.println("Is both strings are equals = "+ country.equals(country1)); } }
Output: Is both strings are equals = true
Is both strings are equals = true
Is both strings are equals = true
Is both strings are equals = false
If you want to compare two strings. Let’s say strings s1 and s2. There is a possibility that string s1 can be a null value. As a good programmer, you should place the string s1 in between brackets(Parameter of equals method). Because, if string s1 has the value null then it will not throw a null pointer exception. It will return the false value.
class ExampleOfEquals { public static void main(String args[]) { String s1 = null; String s2 = "Ravi"; System.out.println("Is strings are equals = " + s2.equals(s1)); // The below statement will throw exception // System.out.println("Is strings are equals = " + s1.equals(s2)); } }
Output: Is strings are equals = false
2. String equalsIgnoreCase() method in java
You can compare two strings by using of equalsIgnoreCase() method. As you know java is case sensitive language and you want to ignore case-sensitive strings. If you just want to compare the string value then you should use the equalsIgnoreCase(). It is also used for String comparison in Java and its returns type is boolean.
firstString.equalsIgnoreCase(secondString);
If both strings are equals it will return true otherwise false. It will compare the original content of both strings.
class ExampleOfEqualsIgnoreCase { public static void main(String args[]) { String name = "Ravi"; String name1 = "ravi"; System.out.println("Is both strings are equals="+name.equalsIgnoreCase(name1)); String address = new String("CHD"); String address1 = new String("ChD"); System.out.println("Is both strings are equals ="+address. equalsIgnoreCase(address1)); String className = "MCa"; String className1 = new String("MCA"); System.out.println("Is both strings are equals = "+ className. equalsIgnoreCase(className1)); String country = "India"; String country1 = "USA"; System.out.println("Is both strings are equals = "+ country. equalsIgnoreCase(country1)); } }
Output: Is both strings are equals = true
Is both strings are equals = true
Is both strings are equals = true
Is both strings are equals = false
3. Object equals(object1, object2) method in java
You can compare 2 strings in java by using of Object.equals(object1, object2) method. This method returns true if both parameters are equal otherwise it will return false. If both strings are null it will return true.
Objects.equals(firstString, secondString);
import java.util.Objects; class ExampleOfEquals { public static void main(String args[]) { String name = "Ravi"; String name1 = "Ravi"; System.out.println("Is both strings are equals = "+ Objects.equals(name, name1)); String address = new String("CHD"); String address1 = new String("CHD"); System.out.println("Is both strings are equals = "+ Objects.equals(address, address1)); String className = "MCA"; String className1 = new String("MCA"); System.out.println("Is both strings are equals="+Objects.equals(className, className1)); String country = "India"; String country1 = "USA"; System.out.println("Is both strings are equals = "+ Objects.equals(country, country1)); } }
Output: Is both strings are equals = true
Is both strings are equals = true
Is both strings are equals = true
Is both strings are equals = false
4. String compareTo() method in java
You can compare two strings by using of String.compareTo() method.
It returns a positive value If the first string is lexicographically greater than the second string.
returns a negative value, If the first string is less than the second string lexicographically.
returns 0, if the first string is lexicographically equal to the second string.
firstString.compareTo(secondString);
class ExampleCompareTo { public static void main(String args[]) { String name = "Ravi"; String name1 = "Ravi"; System.out.println("Is both strings are equals = "+ name.compareTo(name1)); String address = new String("CHD"); String address1 = new String("chd"); System.out.println("Is both strings are equals = "+ address.compareTo(address1)); String className = "hello"; String className1 = new String("flag"); System.out.println("Is both strings are equals = "+ className.compareTo(className1)); String country = "india"; String country1 = "uk"; System.out.println("Is both strings are equals = "+ country.compareTo(country1)); } }
Output: Is both strings are equals = 0
Is both strings are equals = -32
Is both strings are equals = 2
Is both strings are equals = -12
5. == operator in java
This operator is also used to compare the strings. It doesn’t compare the actual value of the string. It compares the reference of string. The main difference between equals() and the == operator is, The equals always compare the value of the string but the == operator compares the references of strings.
class ExampleOfEqualsOperator { public static void main(String args[]) { String name = "Ravi"; String name1 = "Ravi"; System.out.println("Is both strings are equals by use of equals method="+ name.equals(name1)); System.out.println("Is both strings are equals by use of == operator = "+ (name == name1)); String address = new String("CHD"); String address1 = new String("CHD"); System.out.println("Is both strings are equals by use of equals method = "+ address.equals(address1)); System.out.println("Is both strings are equals by use of == operator = "+ (address == address1)); String className = "MCA"; String className1 = new String("MCA"); System.out.println("Is both strings are equals by use of equals method = "+ className.equals(className1)); System.out.println("Is both strings are equals by use of == operator = "+ (className == className1)); } }
Output: Is both strings are equals by use of equals method = true
Is both strings are equals by use of == operator = true
Is both strings are equals by use of equals method = true
Is both strings are equals by use of == operator = false
Is both strings are equals by use of equals method = true
Is both strings are equals by use of == operator = false
Hi javagoal,
Firstly I need to thank your team, I learned a lot new concepts from your site.
In the morning, I have gone through the article “String comparison” from your site, I think the definition of “String.compareTo()” is wrongly given, can you please recheck that. If it is correct then my bad, sorry for that.
Thanks.
Hi Sumith,
Thank you so much for your appreciation. Yes, your observed well there was a mistake now I have corrected them 🙂
The article is fantastic. Every topic gives deep knowledge. but there is an error correct me if I wrong. In example of ” == ” operator the first output should be false.
String name = “Ravi”;
String name1 = “Ravi”;
System.out.println(“Is both strings are equals by use of equals method=”+
name.equals(name1));
System.out.println(“Is both strings are equals by use of == operator = “+ (name ==
name1));
Current Output: Is both strings are equals by use of == operator = true
Correct output: Is both strings are equals by use of == operator = false
Jawed Akhtar…Thanks, Jawed…
But the output is correct because “==” operator gives false, if string has different objects. But here both string will use same reference from string pool.
Please have a look https://javagoal.com/string-class-in-java/