I wanted to clarify if I understand this correctly:
==
-> is a reference comparison, i.e. both objects point to the same memory location.equals()
-> evaluates to the comparison of values in the objects
Am I correct in my understanding ?
I wanted to clarify if I understand this correctly:
==
-> is a reference comparison, i.e. both objects point to the same memory location.equals()
-> evaluates to the comparison of values in the objectsAm I correct in my understanding ?
There are some small differences depending whether you are talking about "primitives" or "Object Types"; the same can be said if you are talking about "static" or "non-static" members; you can also mix all the above...
Here is an example (you can run it):
You can compare the explanations for "==" (Equality Operator) and ".equals(...)" (method in the java.lang.Object class) through these links:
The difference between == and equals confused me for sometime until I decided to have a closer look at it. Many of them say that for comparing string you should use
equals
and not==
. Hope in this answer I will be able to say the difference.The best way to answer this question will be by asking a few questions to yourself. so let's start:
What is the output for the below program:
if you say,
I will say you are right but why did you say that? and If you say the output is,
I will say you are wrong but I will still ask you, why you think that is right?
Ok, Let's try to answer this one:
What is the output for the below program:
Now If you say,
I will say you are wrong but why is it wrong now? the correct output for this program is
Please compare the above program and try to think about it.
Ok. Now this might help (please read this : print the address of object - not possible but still we can use it.)
can you just try to think about the output of the last three lines in the code above: for me ideone printed this out (you can check the code here):
Oh! Now you see the identityHashCode(mango) is equal to identityHashCode(mango2) But it is not equal to identityHashCode(mango3)
Even though all the string variables - mango, mango2 and mango3 - have the same value, which is "mango",
identityHashCode()
is still not the same for all.Now try to uncomment this line
// mango2 = "mang";
and run it again this time you will see all threeidentityHashCode()
are different. Hmm that is a helpful hintwe know that if
hashcode(x)=N
andhashcode(y)=N
=>x is equal to y
I am not sure how java works internally but I assume this is what happened when I said:
java created a string
"mango"
which was pointed(referenced) by the variablemango
something like thisNow in the next line when I said:
It actually reused the same string
"mango"
which looks something like thisBoth mango and mango2 pointing to the same reference Now when I said
It actually created a completely new reference(string) for "mango". which looks something like this,
and that's why when I put out the values for
mango == mango2
, it put outtrue
. and when I put out the value formango3 == mango2
, it put outfalse
(even when the values were the same).and when you uncommented the line
// mango2 = "mang";
It actually created a string "mang" which turned our graph like this:This is why the identityHashCode is not the same for all.
Hope this helps you guys. Actually, I wanted to generate a test case where == fails and equals() pass. Please feel free to comment and let me know If I am wrong.
==
is an operator andequals()
is a method.Operators are generally used for primitive type comparisons and thus
==
is used for memory address comparison andequals()
method is used for comparing objects.==
can be used in many object types but you can useObject.equals
for any type , especially Strings and Google Map Markers.In general, the answer to your question is "yes", but...
.equals(...)
will only compare what it is written to compare, no more, no less.equals(Object o)
method of the closest parent class that has overridden this method.Object#equals(Object o)
method. Per the Object API this is the same as==
; that is, it returns true if and only if both variables refer to the same object, if their references are one and the same. Thus you will be testing for object equality and not functional equality.hashCode
if you overrideequals
so as not to "break the contract". As per the API, the result returned from thehashCode()
method for two objects must be the same if theirequals
methods show that they are equivalent. The converse is not necessarily true.