According the javadoc of Class
Every array also belongs to a class that is reflected as a Class
object that is shared by all arrays with the same element type and
number of dimensions.
But when I run the below
int[] intArray = { 1, 2 };
out.println(intArray.getClass().hashCode());
int[] int2Array = { 1, 2 };
out.println(int2Array.getClass().hashCode());
out.println(intArray.equals(int2Array));
I get the below output
1641745
1641745
false
I am wondering why the equals is returning false even though both the arrays are of int type and have the same dimensions.
This is because you’re calling
equals()on the array instances themselves instead of theirClassobject. Try:You could also write:
As an aside, matching hash codes don’t necessarily indicate equality, though that doesn’t matter here.