DEV Community

Cover image for Comparing Strings
Ritvik Dubey
Ritvik Dubey

Posted on

Comparing Strings

Hello all๐Ÿ˜€ I hope you are doing well. In this article I'll be writing about 4 ways to compare strings in Java. Before continuing this article I would suggest you to go through my last 2 articles, String class and String Methods, if you haven't been through them please check these two short reads.

Let's begin...

Before continuing let's understand why we need to compare Strings, if you want to check some character values or if you are making a simple application where there is login or pin feature you might need to compare strings there. So here are such 4 methods which will be of use while working with String.

1. == operator

Yes the first way to compare Strings is double equal to or equal to equal to (==) relational or comparison operator. This operator is used for checking if the value of two operands is the equal or not. This operator is used to compare two or more than two objects, if they are referring to the same object then it returns true. This operator compares objects references. In short we can say this operator checks values.

public class Demo {
    public static void main(String []args) {
        String str1 = "Lion";
        String str2 = "Lion";
        if(str1 == str2) {
            System.out.println("Same");
        }
        else {
            System.out.println("Not same"); 
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

You can run your code online here

java-double-equals-operator.png

2. equals()

I have already written about this method in my last article, this method returns true if and only if the object is a string and represents the same sequence of characters as this string. Returns true if the current object is the equal as the argument and returns false if objects are not equal. In short this method checks values of both the strings. This time let's understand this with some different example.

public class Demo {
    public static void main(String []args) {
        String str1 = "Lion";
        String str2 = "Lion";
        if(str1.equals(str2)) {
            System.out.println("Same");
        }
        else {
            System.out.println("Not Same");
        }
    }
}

Enter fullscreen mode Exit fullscreen mode

You can run your code online here

java-equals-method.png

3. equalsIgnoreCase()

This method works same as equals() method do, but this method ignores case of the string, it returns true even if the case of string is not same., which means equalsIgnoreCase() method is case insensitive in nature.

public class Demo {
    public static void main(String []args) {
        String str1 = "Lion";
        String str2 = "LiOn";
        if(str1.equalsIgnoreCase(str2)) {
            System.out.println("Same");
        }
        else {
            System.out.println("Not Same");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

You can run your code online here

java-equalsIgnoreCase()-method.png

If same code is executed with equals() method the output will be "Not same".

public class Demo {
    public static void main(String []args) {
        String str1 = "Lion";
        String str2 = "LiOn";
        if(str1.equals(str2)) {
            System.out.println("Same");
        }
        else {
            System.out.println("Not Same");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

You can run your code online here

java-equals-not-ignore.png

4. compareTo()

This method compares strings on the basis of the Unicode value of each character in the strings. This method returns an integer values, these values may be less than, equal, or greater than zero. If two strings are different, they have different characters at same index, or their lengths are different, or both. In short we can say this method checks difference of Unicode values.

Let's understand this better with a few examples-

public class Demo {
    public static void main(String []args) {
        String str1 = "K";
        String str2 = "K";
        System.out.println("The difference is : " + str1.compareTo(str2));
    }
}
Enter fullscreen mode Exit fullscreen mode

You can run your code online here

java-compareTo()-method-1.png

In the above example we compared two similar characters, character K whose Unicode value is 75. Hence the value which got printed is 0, i.e., there is no difference between Unicode values.

public class Demo {
    public static void main(String []args) {
        String str1 = "K";
        String str2 = "k";
        System.out.println("The difference is : " + str1.compareTo(str2));
    }
}
Enter fullscreen mode Exit fullscreen mode

You can run your code online here

java-compareTo()-method-2.png

In the above example we compared two different cases of character K, that is we compared K and k whose Unicode values are 75 and 107. Hence the value which got printed is -32, i.e., difference between Unicode values of K and k.

public class Demo {
    public static void main(String []args) {
        String str1 = "k";
        String str2 = "K";
        System.out.println("The difference is : " + str1.compareTo(str2));
    }
}
Enter fullscreen mode Exit fullscreen mode

You can run your code online here

java-compareTo()-method-3.png

In the above example we compared two different cases of character K, that is we compared K and k whose Unicode values are 75 and 107 but this time we changed the order. Hence the value which got printed is 32, i.e., difference between Unicode values of k and K.

public class Demo {
    public static void main(String []args) {
        String str1 = "AB";
        String str2 = "Ab";
        System.out.println("The difference is : " + str1.compareTo(str2));
    }
}
Enter fullscreen mode Exit fullscreen mode

You can run your code online here

java-compareTo()-method-2.png

In the above example we compared two different strings, AB and Ab in which first character of both the strings are same but there is difference of case in second character. Hence the value which got printed is -32, i.e., difference between Unicode values of B and b and the difference between Unicode values of first character A is same so it didn't affected the final result.

public class Demo {
    public static void main(String []args) {
        String str1 = "Hello";
        String str2 = "Hi";
        System.out.println("The difference is : " + str1.compareTo(str2));
    }
}
Enter fullscreen mode Exit fullscreen mode

You can run your code online here

java-compareTo()-method-5.png

In the above example we compared two different strings, Hello and Hi in which first character of both the strings are same but second character of both the Strings is different, i.e., e and i whose Unicode values are 101 and 105 respectively. Hence the value which got printed is -4, i.e., difference between Unicode values of e and i and the difference between Unicode values of first character H is same so it didn't affected the final result.

For reference you can check Unicode values here

Okay so that's enough for now follow my this journey to learn more about Java.

Thank you for reading.

Please share your thoughts about it and correct me if I'm wrong.

I hope you liked it and found it helpful.

Cover:- Rajat Gour

Connect with me on Twitter or LinkedIn

My personal blog blog.ritvikdubey.com

Top comments (0)