-2

As we know == compare references and equals() compare the content. But after checking equals() implementation in Object class I got confused. the implementation is,

public boolean equals(Object obj) {
        return (this == obj);
    }

as you see the implementation. equals internally calling == operator. Then how it different?

Asif Mushtaq
  • 3,658
  • 4
  • 44
  • 80
  • 1
    Now check the source code of any other equals() implementation. Say: the one in Integer – Gimby Mar 23 '16 at 15:12
  • 2
    They are the same for the default `equals` implementation of `Object`.Not for `String` or other classes overriding the method . – Arnaud Mar 23 '16 at 15:12
  • 1
    if you want to compare equality you are defining your own logic (how should java know what makes two objects logically equal?). That´s where you override `equals`, and define your logic, whereas the standard `Object#equals` can´t have any logical comparision despite comparing if both objects refere to the same instance. – SomeJavaGuy Mar 23 '16 at 15:13
  • Next time I think we should mention String class when comparing `equals()` and `==` mostly time I didn't listened String class reference. that's why I was confuse. Any how now much clear. – Asif Mushtaq Mar 23 '16 at 15:15

2 Answers2

5

They're simply different in that you can override equals(), but cannot override == in any way.

So while equals() may check for logical equality of two objects if the class author decides so, == will always compare the reference and thus be true only if both operands are the same object (or same primitive value).

Jiri Tousek
  • 12,211
  • 5
  • 29
  • 43
  • I don't know why I got -1 in my question, first that was 4. But you really provide exact difference..!! Thanks. – Asif Mushtaq Mar 23 '16 at 15:22
  • You got down voted, because this question is asked before. – martijnn2008 Mar 23 '16 at 15:24
  • Possibly because a quick search on this site would give you the answer without a need to post a new question. – Jiri Tousek Mar 23 '16 at 15:24
  • @JiriTousek can you give me link where already this question? I don't think so it asked before with same content/meaning that I wrote. – Asif Mushtaq Mar 23 '16 at 15:27
  • This question was marked as a duplicate of one such question - answers to that one describe quite thoroughly how `equals()` works in relation with `==`, and why and when the two return the same value. – Jiri Tousek Mar 23 '16 at 15:29
  • @JiriTousek Ok that could be my mistake to understand. Any how thanks I got the answer next time I will care. – Asif Mushtaq Mar 23 '16 at 15:31
0

The equals() method compares the "value" inside String instances (on the heap) irrespective if the two object references refer to the same String instance or not. If any two object references of type String refer to the same String instance then great! If the two object references refer to two different String instances .. it doesn't make a difference. Its the "value" (that is: the contents of the character array) inside each String instance that is being compared.

On the other hand, the "==" operator compares the value of two object references to see whether they refer to the same String instance. If the value of both object references "refer to" the same String instance then the result of the boolean expression would be "true"..duh. If, on the other hand, the value of both object references "refer to" different String instances (even though both String instances have identical "values", that is, the contents of the character arrays of each String instance are the same) the result of the boolean expression would be "false".

Anokim
  • 19
  • 1
  • 5