Why is the following code printing "Different."
?
boolean[][] a = { {false,true}, {true,false} };
boolean[][] b = { {false,true}, {true,false} };
if (Arrays.equals(a, b) || a == b)
System.out.println("Equal.");
else
System.out.println("Different.");
Why is the following code printing
"Different."
?
Because Arrays.equals
performs a shallow comparison. Since arrays inherit their equals
-method from Object
, an identity comparison will be performed for the inner arrays, which will fail, since a
and b
do not refer to the same arrays.
If you change to Arrays.deepEquals
it will print "Equal."
as expected.