简体   繁体   English

java.lang.Class和相等

[英]java.lang.Class and equality

According the javadoc of Class 根据Class的javadoc

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. 每个数组也属于一个类,它反映为一个Class对象,由具有相同元素类型和维数的所有数组共享。

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. 我想知道为什么equals返回false,即使这两个数组都是int类型并具有相同的维度。

This is because you're calling equals() on the array instances themselves instead of their Class object. 这是因为您在数组实例本身而不是Class对象上调用equals() Try: 尝试:

out.println(intArray.getClass().equals(int2Array.getClass())); //prints true

You could also write: 你也可以这样写:

out.println(int[].class.equals(int[].class)); //prints true thankfully

As an aside, matching hash codes don't necessarily indicate equality, though that doesn't matter here. 另外,匹配的哈希码并不一定表示相等,尽管这并不重要。

尝试调用intArray.getClass().equals(int2Array.getClass())

In general, the Java hash code contract only requires that: 通常,Java哈希代码契约只要求:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. 每当在执行Java应用程序期间多次在同一对象上调用它时,hashCode方法必须始终返回相同的整数,前提是不修改对象上的equals比较中使用的信息。 This integer need not remain consistent from one execution of an application to another execution of the same application. 从应用程序的一次执行到同一应用程序的另一次执行,该整数不需要保持一致。
  • If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result. 如果两个对象根据equals(Object)方法相等,则对两个对象中的每一个调用hashCode方法必须生成相同的整数结果。
  • It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. 如果两个对象根据equals(java.lang.Object)方法不相等,则不需要在两个对象中的每一个上调用hashCode方法必须生成不同的整数结果。 However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables. 但是,程序员应该知道为不等对象生成不同的整数结果可能会提高哈希表的性能。

(From the Java documentation on Object#hashCode ) (来自Object#hashCode的Java文档

Here you have two integer arrays that are not equal (eg a.equals(b) => false ), but they are not (see the third point) required to return unequal hashcodes. 这里有两个不相等的整数数组(例如a.equals(b) => false ),但它们不是 (参见第三点)返回不相等的哈希码所需的。


Also, note that your code will work if you use Arrays.equals instead of Object#equals , as follows. 另请注意,如果使用Arrays.equals而不是Object#equals ,则代码将起作用,如下所示。 Note that Arrays.equals checks that "both arrays contain the same number of elements, and all corresponding pairs of elements in the two arrays are equal." 注意, Arrays.equals检查“两个数组包含相同数量的元素,并且两个数组中所有相应的元素对相等。”

int[] intArray = { 1, 2 };
out.println(intArray.getClass().hashCode());
int[] int2Array = { 1, 2 };
out.println(int2Array.getClass().hashCode());

out.println(Arrays.equals(intArray, int2Array));

See http://www.ideone.com/HaysD for a working example. 有关工作示例,请访问http://www.ideone.com/HaysD

You are comparing two different arrays. 您正在比较两个不同的阵列。 Array equality is based on identity, not array content. 数组相等性基于标识,而不是数组内容。 Since they are not the same array, the result is false. 由于它们不是同一个数组,因此结果是错误的。

If you want to test the content of two arrays for equality, there are helper methods in java.util.Arrays . 如果要测试两个数组的内容是否相等,则java.util.Arrays有辅助方法。

out.println(Arrays.equals(intArray, int2Array);

The default implementation of equals() is defined by Object.equals(Object) : equals()的默认实现由Object.equals(Object)定义:

The equals method for class Object implements the most discriminating possible equivalence relation on objects; 类Object的equals方法实现了对象上最具辨别力的等价关系; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true). 也就是说,对于任何非空引用值x和y,当且仅当x和y引用同一对象时,此方法才返回true(x == y的值为true)。

When you compare two arrays using equals() , theis default method is invoked. 使用equals()比较两个数组时,将调用默认方法。

Run this and the answer will be clear: 运行这个,答案将是明确的:

int[] array1 = { 1, 2 };
int[] array2 = { 1, 2 };

System.out.println("array1.hashcode: " + array1.hashCode());
System.out.println("array2.hashcode: " + array2.hashCode());
System.out.println("array1.class.hashcode: " + array1.getClass().hashCode());
System.out.println("array2.class.hashcode: " + array2.getClass().hashCode());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM