java这个方法如何计算Rational(1)。hashCode()?1的分子或分母本身就是1
/**
* Returns a hash code for this Rational object. That hash code is
* derived from the hash codes for its two components. This design
* decision ensures that two Rational objects that are equal will have
* the same hash code.
* @return An integer hash code for this object
*/
public int hashCode() {
return new Integer(num).hashCode() ^ (37 * new Integer(den).hashCode());
}
# 1 楼答案
类
Rational
不是标准Java的一部分。谷歌搜索显示它可能是一个外部库的一部分,但你还没有告诉我们是哪一个,所以我们无法真正说明它的行为如果
Rational
有一个构造函数接受一个int
,我希望它提供一个默认分母1
,因此使用您引用的方法计算哈希代码是完全合理的默认的
Integer#hashCode()
方法只是将整数本身作为散列码返回,因此返回的值将是0x00000001
与0x00000025
或0x00000024
(36位小数)异或