We all know if we create two String objects and use == to compare them it will return false and if we use equals to method it will return true. But by default equals method implement == only , then how does it return true , it should return whatever == is returning ??
-
equals method is overridden in String class. That's why. – AmitG Mar 23 '13 at 10:20
5 Answers
Yes by default equals method implements ==
in Object
class . But you can Override the equals
method in your own class to change the way equality
is done between two objects of the same class. For example the equals
method in String
class is overridden as follows:
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = count;
if (n == anotherString.count) {
char v1[] = value;
char v2[] = anotherString.value;
int i = offset;
int j = anotherString.offset;
while (n-- != 0) {
if (v1[i++] != v2[j++])
return false;
}
return true;
}
}
return false;
}
So this is the reason that for the following code:
String s1 = new String("java");
String s2 = new String("java");
s1==s2
returns false
since both are referencing different objects on heap. Whereas s1.equals(s2)
returns true
since now the equals
being called is what defined within String
class where String
objects are compared on the basis of contents
of String.

- 12,976
- 2
- 27
- 38
-
1No by default `equals` doesn't implement `==` for String class. And his question was how even if equals implement `==` they both behave differently! – Amar Mar 23 '13 at 10:31
-
@Amar: Go and re-read the question and think maturely before downvoting someone's answer. OP is asking that since `equals` method by default checks for `==` , then how come that `equals` method for two different `String` with same contents returning `true`. And that's what I have explained the reason for this in my answer.. Re-read my answer too.. – Vishal K Mar 23 '13 at 10:34
-
Maybe! What I understood from the question that he has actually seen the String's implementation and got confused by the initial `==` check in the method. If that understanding is wrong I will certainly take back my downvote. BTW, no need to get personal out here and comment on my maturity. Chill. – Amar Mar 23 '13 at 10:40
-
@Amar: If OP had gone through the `equals` method source code within `String` class , I am sure he wouldn't have raised this question. Because within `equals` method of `String` class after `==` fails , some other checking are also done..And that would have sure been noticed by the OP. – Vishal K Mar 23 '13 at 10:48
-
-
I attempted to edit the answer and change `s1==s1` to `s1==s2` but it wasn't a significant enough change for the edit to occur immediately. I think that the answer is improved with that typo corrected. – gcbound Mar 03 '14 at 04:51
the equals method in the String class is overridden and it tries to check if all the characters in both the Strings are equal or not. If found then it returns true. So the behavior of equals method in String class is different from the normal object class implementation of it.

- 7,746
- 2
- 28
- 38
equals
method is originally a method of Object
class. And every class in Java extends the Object
class by default. Now, equals
method is overridden for String
class to act differently than ==
.
It's javadoc explains it perfectly:
Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.
It's implementation goes as follows:
@override
public boolean equals(Object anObject) {
// This check is just for the case when exact same String object is passed
if (this == anObject) {
return true;
}
// After this only real implementation of equals start which you might be looking for
// For other cases checks start from here
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = count;
if (n == anotherString.count) {
char v1[] = value;
char v2[] = anotherString.value;
int i = offset;
int j = anotherString.offset;
while (n-- != 0) {
if (v1[i++] != v2[j++])
return false;
}
return true;
}
}
return false;
}

- 11,930
- 5
- 50
- 73
.equals()
checks if the strings are identical ei. have the same characters. ==
only checks if pointers point to the same objects. You can have different objects with the same characters, thats why you should use .equals()
to compare them

- 13,134
- 6
- 58
- 106
String
class in java overrides the equals
method of Object
class such that it compares the content of the two strings rather than comparing the references(default implementation of in Object
class).
See below the equals
method implementation of String
class:
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = count;
if (n == anotherString.count) {
char v1[] = value;
char v2[] = anotherString.value;
int i = offset;
int j = anotherString.offset;
while (n-- != 0) {
if (v1[i++] != v2[j++])
return false;
}
return true;
}
}
return false;
}

- 15,488
- 8
- 55
- 83