9
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.
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.
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");
}
}
}
You can run your code online here
![](https://res.cloudinary.com/practicaldev/image/fetch/s--1O2fJ7ky--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1627201698942/lRzi4qVJY.png)
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");
}
}
}
You can run your code online here
![](https://res.cloudinary.com/practicaldev/image/fetch/s--X5e1dnHI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1627202401001/CXjYTDYyG.png)
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");
}
}
}
You can run your code online here
![](https://res.cloudinary.com/practicaldev/image/fetch/s--SbuBz97R--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/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".
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://res.cloudinary.com/practicaldev/image/fetch/s--TwBhrfd4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1627203033795/UwCgJ1s54.png)
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));
}
}
You can run your code online here
![](https://res.cloudinary.com/practicaldev/image/fetch/s--Itaj_M58--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/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.
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://res.cloudinary.com/practicaldev/image/fetch/s--oKfpw5z5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/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.
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://res.cloudinary.com/practicaldev/image/fetch/s--2vvgvUQ6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/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.
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://res.cloudinary.com/practicaldev/image/fetch/s--xVkAxDnl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/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.
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://res.cloudinary.com/practicaldev/image/fetch/s--Wn8OtsD2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/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
Cover:- Rajat Gour
My personal blog blog.ritvikdubey.com
9