How to compare two Strings in Java
In Java, Strings are immutable. That means whenever you try to change/modify the string you get a new instance. You cannot change the original string. This has been done so that these string instances can be cached.
A typical program contains a lot of string references and caching these instances can decrease the memory footprint and increase the performance of the program.
When using == operator for string comparison you are not comparing the contents of the string, but are actually comparing the memory address. If they are both equal it will return true and false otherwise. Whereas equals in string compare the string contents.
In this article, we are going to see how to compare two strings in java without equals, how to compare characters in a string in java, How to compare two Strings in Java using equals method of Object class, and how to compare Strings using compareTo() method.
1). Compare using "==" Operator
If you want to compare two string references We can use == operators for reference comparison.
public class UsingEqualToOperaor {
public static void main(String args[])
{
String str1 = "Hello World";
String str2 = "Hello World";
String str3 = new String("Hello World");
System.out.println(str1 == str2);
System.out.println(str1 == str3);
}
}
output:
true
false
This s because str1 and str2 point on the same location to return true but str3 its created on the heap so it has a different memory location, so return false
2). Compare using .equals() method of object class
In java equals() method of object class is overridden for the content comparison,
If we compare two strings uses the equals method, then they are compared based on the content.
public class UsingEquals {
public static void main(String args[])
{
String str1 = new String("Anna");
String str2 = new String("Java");
String str3 = new String("Anna");
String str4 = new String("java");
System.out.println("Compare string str1 and str3 : "+ str1.equals(str3));
System.out.println("Compare string str2 and str4 : "+str2.equals(str4));
System.out.println("Compare string str1 and str4 : ");
System.out.println( str1==str4);
//false references are different
}
}
output:
Compare string str1 and str3 : true
Compare string str2 and str4 : false
Compare string str1 and str4 :
false
This is because str1 and str3 has same content so return true
but str2 and str4 has a different content, so return false
we need to use equalsIgnoreCase() method for that.
3). Compare using String.equalsIgnoreCase() of String class
This method from the String class compares two strings irrespective of the case (lower or upper) of the string. It returns true if the argument is not null and the contents of both the Strings are the same ignoring case, else return false.
str2.equalsIgnoreCase(str1);
So in the above program
If we use
System.out.println("Compare string str2 and str4"+str2.equalsIgnoreCase(str4)); //true
It will return because we ignore the case of Strings then content are the same.
4). Compare using Objects.equals() of Object class
Object.equals(Object a, Object b)
- This method returns true if the arguments are equal to each other and false otherwise.
- If both arguments are null, it returns true
- If exactly one argument is null, it returns false
- Otherwise, equality is determined by using the equals() method of the first argument.
import java.util.*;
public class CompareUsingObjectsEquals {
public static void main(String args[])
{
String str1 = new String("Anna");
String str2 = new String("Java");
String str3 = new String("Anna");
String str4 = null;
String str5 = null;
System.out.println("Compare string str1 and str3 : "+ Objects.equals(str1, str3));
System.out.println("Compare string str2 and str3 : "+Objects.equals(str2, str3));
System.out.println("Compare string str2 and str4 : "+ Objects.equals(str2, str4));
System.out.println("Compare string str1 and str5 : "+ Objects.equals(str1, str5));
System.out.println("Compare string str4 and str5 : "+ Objects.equals(str4, str5));
}
}
output:
Compare string str1 and str3 : true
Compare string str2 and str3 : false
Compare string str2 and str4 : false
Compare string str1 and str5 : false
Compare string str4 and str5 : true
5). Compare using Str1.compareTo(str2) method of String class
This method compares two string based on the following logic
- if (string1 > string2) it returns a positive value.
- if both the strings are equal lexicographically
- i.e.(string1 == string2) it returns 0.
- if (string1 < string2) it returns a negative value.
import java.util.*;
public class UsingCompareTo {
public static void main(String args[])
{
String str1 = new String("Anna");
String str2 = new String("Java");
String str3 = new String("Anna");
String str4 = new String("java");
System.out.println("Compare string str1 and str3 : "+ str1.compareTo(str3));
System.out.println("Compare string str2 and str3 : "+str2.compareTo(str3));
System.out.println("Compare string str2 and str4 : "+ str2.compareTo(str4));
System.out.println("Compare string str3 and str4 : "+ str3.compareTo(str4));
}
}
output:
Compare string str1 and str3 : 0
Compare string str2 and str3 : 9
Compare string str2 and str4 : -32
Compare string str3 and str4 : -41
Relationship between .equals() method and ==(double equal operator)
- If r1==r2 is true then r1.equals(r2) is always true i.e., if two objects are equal by == operator then these objects are always equal by .equals( ) method also.
- If r1==r2 is false then we can't conclude anything about r1.equals(r2) it may return true (or) false.
- If r1.equals(r2) is true then we can't conclude anything about r1==r2 it may returns true (or) false.
- If r1.equals(r2) is false then r1==r2 is always false.
Differences between == (double equal operator) and .equals() method?
== (double equal operator) | .equals() method | |
1. | It is an operator that applies to both primitives and object references. | It is a method applicable only for object references but not for primitives. |
2. | In the case of primitives == (double equal operator) meant for content comparison, but in the case of object references == operator meant for reference comparison. | By default .equals() method present in object class is also meant for reference comparison. |
3. | We can't override== operator for content comparison in object references. | We can override .equals() method for content comparison. |
4. | If there is no relationship between argument types then we will get a compile-time error saying incompatible types. (relation means child to parent or parent to child or same type) | If there is no relationship between argument types then .equals() method simply returns false and we won't get any compile-time error and runtime error. |
5. | For any object reference r, r==null is always false. | For any object reference r, r.equals(null) also return false. |
String s = new String("Anna");
StringBuffer sb = new StringBuffer("Anna");
System.out.println(s == sb); // CE : incomparable types : String and StringBuffer
System.out.println(s.equals(sb)); //false
That's it, We have seen How to compare two Strings in Java.
0 Comments
Post a Comment