Java String comparison. The difference between == and equals().
Feb 23
Many Java beginners find it difficult to differentiate between == operator and the “equals()” method when comparing String variables in Java. They assume that both operations perform the same function and either one can be used to compare two string variables. I have even seen many experienced programmers committing the mistake of using “==” to compare the values of two strings.
So what is the difference between “==” and “equals()” and what is the correct method of comparing two string variables?
The == operator:
The “==” operator compares the references of two objects in memory. It returns true if both objects point to exact same location in memory. Remember that Strings are immutable in Java, so if you have a String variable str1 with a value of “abc” and then you create another variable str2 with value “abc”, rather than creating a new String variable with same value, Java will simply point str2 to same memory location that holds the value for str1.
In this scenario str1==str2 will return true because both str1 and str2 are referencing the same object in memory.
However, if you create a new variable using the String constructor, like String str3=new String(“abc”); Java will allocate new memory space for str3. Now although str1 and str3 have exact same value, str1==str3 will return false because they are two distinct objects in the memory.
The equals() method:
The equals method compare the text content or the value of two string variables. If both variables have exact same value, equals() will return true, otherwise it will return false.
So, str1.equals(str2) , str1.equals(str3), and str2.equals(str3) will all return true.
str1.equals(“xyz”) will return false.
Here is an example to help you understand the difference between “==” and equals().
public class StringComparison{
public static void main(String args[]){
String str1 = new String("abc");
String str2 = new String ("abc");
if(str1==str2){
System.out.println("str1==str2 is true");
}else{
System.out.println("str1==str2 is false");
}
if(str1.equals(str2)){
System.out.println("str1.equals(str2) is true");
}else{
System.out.println("str1.equals(str2) is false");
}
String str3="abc",
str4 ="abc";
if(str3==str4){
System.out.println("str3==str4 is true");
}else{
System.out.println("str3==str4 is false");
}
}
}
Here is the output from this program.
str1==str2 is false
str1.equals(str2) is true
str3==str4 is true
Most of the time you need to compare the values of the variable so you will need the equals() method. In situations where you need to check if two variables reference same memory location then you will use “==” operator.
I hope this will help you understand when to use “==” or equals() for Java string comparison.
Image credit: Martyne
Related posts:



Apr 27, 2010 @ 07:09:45
This post is really good I haven’t this problems with C language but I had any problems to differentiate these operators in java, I never have used the function equals() now I will use it. Thanks.
Jun 02, 2010 @ 09:56:16
I like this post!
very good for beginner like me! thanX again!
Jun 21, 2010 @ 23:45:32
it is very good post! it helps a lot to a beginner
Aug 07, 2010 @ 09:37:06
Most of the time you need to compare the values of the variable
Oct 19, 2010 @ 12:01:47
I assumed that both operations perform the same function and either one can be used to compare two string variables but it turns out I was wrong.
Nov 10, 2010 @ 06:39:26
its very good ,thanks
Feb 11, 2011 @ 08:04:33
Just to add a bit more to this article, the equals() method is often gets overriden in order to set-up for specific comparaaison purpouses that will takes place the on the instances of the class, where the overriding takes place.
It is still a good article, and if you are likely to do SCJP then most probably you will be questioned about the right use of equals()…
Jul 19, 2011 @ 01:40:34
Is there any opposite function available of equals()?
Jul 21, 2011 @ 12:52:25
@Zamshed: try the ! operator
Aug 09, 2011 @ 07:12:29
>> str4 =”abc”;
Shouldn’t it be: String str4 =”abc”; ?
Or is it that I am missing something here?
Aug 09, 2011 @ 07:15:27
OK got it, I missed the presence of comma in the previous line
Aug 10, 2011 @ 02:40:02
Thanks a lot..very clear explanation than other posts in internet on the same issue.
Sep 12, 2011 @ 05:17:37
Thank you so much for a great explanation. It is a great help to the beginners. I would like to add the following lines to the explanation as well:
equals() and hashcode() methods are from Cosmic super class Object from Java.lang package and hence they’re the one’s those are overridden most of the times.
Do let me know if I’m wrong…
Thanks and Regards,
Ashish
Sep 26, 2011 @ 07:28:14
String s1=”abc”;
if(s1==”abc”){
System.out.println(“Both are Equal”);
}else{
System.out.println(“Both are not Equal”);
}
Output:- Both are Equal
How this can Happen can any one please explain ?
Sep 26, 2011 @ 14:22:40
I haven’t looked at the Java source code, but my educated guess is that the Java run time is placing all the constant strings (i.e. “abc”) at specific locations in memory, and then assigning the pointers (i.e. s1) to point to those locations. Both of the “abc” constants, being equal, would (as a optimization) be assigned to the same location, Since s1 was assigned to point to that location, s1 == “abc” will be true. If you built up the string “abc” in another fashion, this would not be the case,
Tom
Sep 30, 2011 @ 13:07:37
Ok, the equals() method makes sense, but what if you cant use the == or <= to compare a variable? How do you use equals() to test a range of values, assuming you can at all?
Oct 01, 2011 @ 00:28:09
Hi,
Thanks for the post. I am just a beginner in Java.
I need to know how to validate multiple values for a string.
like, if the value of month is not equal to J or F or M, perform some error handling.
The requirement is: J, F and M are the expected (good) values for month.
Example: If (!month.equals(“J”) || !month.equals(“F”) || !month.equals(“A”)) {
Perform error validation;
}
Is the above correct?
Any help is appreciated. Thanks
Oct 01, 2011 @ 01:02:10
Or can we say?
If (!month.equals(“J”, “F”,“A”)) {
Perform error validation;
}
Oct 21, 2011 @ 05:18:05
So, what is the diff between:
String a = “abc”;
String a = new String(“abc”);
Beacuse “everything in Java is an object” the first one above is an object of String class, the same as the latter. Is there any difference?
Nov 16, 2011 @ 05:47:29
Before reding your post i had confision to use these operators. Now i got clear idea. Thanks for your post..
Nov 23, 2011 @ 14:40:33
Hi .. Thanks mate for your posting ..
..
It’s help me a lot
Love it ..
Jan 26, 2012 @ 04:05:01
thanx a lot ……… i was breaking my head as i did the same mistake of confusing == ……..
your article helped ………
Jan 30, 2012 @ 08:07:18
Wow, I never knew that _this_ is the difference between both compare operations. Thanks for the info! Much appreciated