java. Lang. class and equal
•
Java
According to the Javadoc of the class
But when I ran down there
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 following output
1641745 1641745 false
I want to know why equals returns false, even if both arrays are of type int and have the same dimension
Solution
This is because you called equals () on array instances instead of their class objects Try:
out.println(intArray.getClass().equals(int2Array.getClass())); //prints true
You can also write:
out.println(int[].class.equals(int[].class)); //prints true thankfully
In addition, matching hash codes do not necessarily represent equality, although this is not important
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码