在整数对象的情况下,== 和 equal 如何在 java 中工作?
How == and equal works in java in Case of Integer Object?
我发现了很多可能重复的问题,但 none 澄清了我对它如何运作的疑问?
Integer a =25654; // a.hashCode()=>25654
Integer b =25654; // b.hashCode()=>25654
System.out.println(a.equals(b)); => true
System.out.println(a == b); => false
我在某处看过这个答案..
If no parent classes have provided an override, then it defaults to the method from the ultimate parent class, Object, and so you're left with the Object#equals(Object o) method. Per the Object API this is the same as ==; that is, it returns true if and only if both variables refer to the same object, if their references are one and the same. Thus you will be testing for object equality and not functional equality.
在这种情况下,两个对象具有相同的内存地址(根据哈希码)仍然为什么当我们使用 == 进行比较时它 returns 为假?或者实际的内存地址不同?如果我错了,请纠正我。谢谢
The hashCode method defined by class Object does return distinct
integers for distinct objects.
但是 Integer.hashCode
覆盖 Object.hashCode
:
returns a hash code value for this object, equal to the primitive int
value represented by this Integer object.
这意味着具有相同 hashCode 的整数不必共享相同的对象。
Integer.hashCode
的源代码
@Override
public int hashCode() {
return Integer.hashCode(value);
}
public static int hashCode(int value) {
return value;
}
如果你让:
Integer a = 127;
Integer b = 127;
System.out.println(a.equals(b)); // true
System.out.println(a == b); // true
这是因为可以缓存 [-128, 127] 之间的整数。
in this case both object have same memory address(as per hashcode)
still why does it returns false when we compare using == ? or the
actual memory address is different? please correct me if i am wrong.
Thanks
哈希码不必被视为内存地址,因为对象的内部地址可能会随时间变化。
hashCode()
的规范从未声明具有相同 hashcode 值的两个对象必然引用同一个对象。
即使是具有相同哈希码的两个对象在 Object.equals()
方面也可能不是 equals
。
但规范指出:
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.
这在您的示例中是可以验证的:
Integer a =25654; // a.hashCode()=>25654
Integer b =25654; // a.hashCode()=>25654
System.out.println(a.equals(b)); => true
a
和 b
指的是 equals()
的对象,是的,它们的哈希码确实相同。
您的引用不是指 hashCode()
,而是指默认的 Object.equals()
方法,如果没有对 equals()
进行覆盖,将使用该方法。
例如,如果我不为 Foo 覆盖 equals() 和 hashCode() class :
Foo fooOne = new Foo(1);
Foo fooOneBis = new Foo(1);
fooOne.equals(fooOneBis); // return false
这两个对象具有相同的状态,但它们并不相等,因为在幕后使用 Object.equals()
比较对象本身而不是它们的状态。
确实 equal()
只有当变量引用同一个对象时才会 return 为真 :
Foo foo = new Foo(1);
foo.equals(foo); // return true
我发现了很多可能重复的问题,但 none 澄清了我对它如何运作的疑问?
Integer a =25654; // a.hashCode()=>25654
Integer b =25654; // b.hashCode()=>25654
System.out.println(a.equals(b)); => true
System.out.println(a == b); => false
我在某处看过这个答案..
If no parent classes have provided an override, then it defaults to the method from the ultimate parent class, Object, and so you're left with the Object#equals(Object o) method. Per the Object API this is the same as ==; that is, it returns true if and only if both variables refer to the same object, if their references are one and the same. Thus you will be testing for object equality and not functional equality.
在这种情况下,两个对象具有相同的内存地址(根据哈希码)仍然为什么当我们使用 == 进行比较时它 returns 为假?或者实际的内存地址不同?如果我错了,请纠正我。谢谢
The hashCode method defined by class Object does return distinct integers for distinct objects.
但是 Integer.hashCode
覆盖 Object.hashCode
:
returns a hash code value for this object, equal to the primitive int value represented by this Integer object.
这意味着具有相同 hashCode 的整数不必共享相同的对象。
Integer.hashCode
@Override
public int hashCode() {
return Integer.hashCode(value);
}
public static int hashCode(int value) {
return value;
}
如果你让:
Integer a = 127;
Integer b = 127;
System.out.println(a.equals(b)); // true
System.out.println(a == b); // true
这是因为可以缓存 [-128, 127] 之间的整数。
in this case both object have same memory address(as per hashcode) still why does it returns false when we compare using == ? or the actual memory address is different? please correct me if i am wrong. Thanks
哈希码不必被视为内存地址,因为对象的内部地址可能会随时间变化。
hashCode()
的规范从未声明具有相同 hashcode 值的两个对象必然引用同一个对象。
即使是具有相同哈希码的两个对象在Object.equals()
方面也可能不是equals
。
但规范指出:
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.
这在您的示例中是可以验证的:
Integer a =25654; // a.hashCode()=>25654
Integer b =25654; // a.hashCode()=>25654
System.out.println(a.equals(b)); => true
a
和 b
指的是 equals()
的对象,是的,它们的哈希码确实相同。
您的引用不是指 hashCode()
,而是指默认的 Object.equals()
方法,如果没有对 equals()
进行覆盖,将使用该方法。
例如,如果我不为 Foo 覆盖 equals() 和 hashCode() class :
Foo fooOne = new Foo(1);
Foo fooOneBis = new Foo(1);
fooOne.equals(fooOneBis); // return false
这两个对象具有相同的状态,但它们并不相等,因为在幕后使用 Object.equals()
比较对象本身而不是它们的状态。
确实 equal()
只有当变量引用同一个对象时才会 return 为真 :
Foo foo = new Foo(1);
foo.equals(foo); // return true