We are about to switch to a new forum software. Until then we have removed the registration on this forum.
hello,
can anyone explain how does equals()
works for Object ?
I understand that Object are equals only if they share the same hashCode ?
and so to explain hashCode & overide thing (if it is linked)
https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#clone()
thank you
String us[] = {"a", "b", "c"};
String them[] = us.clone() ;
println("us hashcode : "+us.hashCode() );
println("them hashcode : "+them.hashCode() );
println(us.equals(them)) ; // return false
Answers
==
. That is, it compares the values of 2 references.false
for diff. objects, regardless whether both got exactly the same content.new
object w/ the same content as the original.@Override
equals(). Therefore cloned arrays are alwaysfalse
when compared against the original or even to each other, b/c each 1 got a unique memory address.static
method equals() from class Arrays can do the job instead: :-bdhttp://docs.Oracle.com/javase/8/docs/api/java/util/Arrays.html#equals-java.lang.Object:A-java.lang.Object:A-
println(java.util.Arrays.equals(us, them)); // returns true
okay and hasCode() return the reference of the object ?
Better read their explanation for it:
https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#hashCode--
okay. lets said a "representation" of the reference (or pointer) of the Object.
thx u , its clearer