Now I see how to implement a method hashCode() for the Boolean object:
public int hashCode() {
return value ? 1231 : 1237;
}
And I wonder why it returns a values 1231 or 1237, why not something else?
Now I see how to implement a method hashCode() for the Boolean object:
public int hashCode() {
return value ? 1231 : 1237;
}
And I wonder why it returns a values 1231 or 1237, why not something else?
These two numbers are sufficiently big prime numbers. Please read the article on Hash Table on Wikipedia for further info.
1231 and 1237 are just two (fairly large) arbitrary prime numbers. Any other two large prime numbers would do fine.
They are well suited as hash codes as it is unlikely that they accidentally map to the same bucket in, say, a hash set. The greatest common divisor of the number of buckets and some large prime-number is 1 (unless the number of buckets happens to equal a multiple of the prime number in question).
Besides, this only matters if you're storing Booleans together with other objects, since Booleans only have two distinct instances (two different Boolean values will always return unique hash-codes) or when using them in a larger, composite, hash code.