Comparing Strings
Tags: java, beginners, discuss, javascript
Published: 2021-07-25 10:12:02

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](https://dev.to/ritvikdubey27/string-class-in-java-4jb7)*** and  ***[String Methods](https://dev.to/ritvikdubey27/string-methods-9i8)***, 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. 

```Java
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");	
	    }
    }
}
``` 
***You can run your code online [here](https://www.tutorialspoint.com/compile_java_online.php)***

![java-double-equals-operator.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1627201698942/lRzi4qVJY.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.

```Java
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");
	    }
    }
}

``` 
***You can run your code online [here](https://www.tutorialspoint.com/compile_java_online.php)***

![java-equals-method.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1627202401001/CXjYTDYyG.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.

```Java
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");
	    }
    }
}
``` 
***You can run your code online [here](https://www.tutorialspoint.com/compile_java_online.php)***

![java-equalsIgnoreCase()-method.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1627203230870/T7nKMCP1-.png)


If same code is executed with **equals()** method the output will be **"Not same"**.
```Java
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");
	    }
    }
}
``` 
***You can run your code online [here](https://www.tutorialspoint.com/compile_java_online.php)***

![java-equals-not-ignore.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1627203033795/UwCgJ1s54.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-
```Java
public class Demo {
    public static void main(String []args) {
        String str1 = "K";
        String str2 = "K";
        System.out.println("The difference is : " + str1.compareTo(str2));
    }
}
``` 
***You can run your code online [here](https://www.tutorialspoint.com/compile_java_online.php)***

![java-compareTo()-method-1.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1627205739068/aX2NPJ-fb.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.

```Java
public class Demo {
    public static void main(String []args) {
        String str1 = "K";
        String str2 = "k";
        System.out.println("The difference is : " + str1.compareTo(str2));
    }
}
``` 
 ***You can run your code online [here](https://www.tutorialspoint.com/compile_java_online.php)***

![java-compareTo()-method-2.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1627206035321/-Mv8P-oqk.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.

```Java
public class Demo {
    public static void main(String []args) {
        String str1 = "k";
        String str2 = "K";
        System.out.println("The difference is : " + str1.compareTo(str2));
    }
}
``` 
***You can run your code online [here](https://www.tutorialspoint.com/compile_java_online.php)***

![java-compareTo()-method-3.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1627206349190/tEsuWW4j99.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.

```Java
public class Demo {
    public static void main(String []args) {
        String str1 = "AB";
        String str2 = "Ab";
        System.out.println("The difference is : " + str1.compareTo(str2));
    }
}
``` 
***You can run your code online [here](https://www.tutorialspoint.com/compile_java_online.php)***

![java-compareTo()-method-2.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1627206584953/UwtdRgWjL.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.

```Java
public class Demo {
    public static void main(String []args) {
        String str1 = "Hello";
        String str2 = "Hi";
        System.out.println("The difference is : " + str1.compareTo(str2));
    }
}
```
***You can run your code online [here](https://www.tutorialspoint.com/compile_java_online.php)*** 

![java-compareTo()-method-5.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1627207001739/bz82-SoeK.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](https://www.rapidtables.com/code/text/ascii-table.html)*** 


### 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](https://twitter.com/rajatgour20)***
#### *Connect with me on [Twitter](https://twitter.com/ritvikdubey27?s=09) or [LinkedIn](https://www.linkedin.com/in/ritvik-dubey-3bbb22169)*
#### *My personal blog [blog.ritvikdubey.com](https://blog.ritvikdubey.com/)*