Question about arrays in Java -
can explain why, a.equals(b)
false
, when initiate b
using int[] b = a.clone()
true
if initiate b
using int[] b = a
?
int[] = {1, 2, 3, 4, 5}; int[] b = a; //int[] b = a.clone(); if(a==b){//true system.out.println("equal"); } if(a.equals(b)){//true system.out.println("equal"); }
well if use
int[] b = a;
then b
, a
refer same object, trivially they're equal. first comparison (==) certainly return false between a
, a.clone()
values refer different objects. sounds arrays don't override equals
(e.g. arraylist
does), hence clone not being equal original under equals
method either.
edit: indeed, language specification section 10.7, array members:
all members inherited class object; method of object not inherited clone method.
in other words, array overrides clone()
not tostring
/ hashcode
/ equals
.
Comments
Post a Comment