The JavaDoc of Boolean.hashCode()
method says:
Returns the integer
1231
if this object representstrue
; returns the integer1237
if this object representsfalse
.
Question
In the below code, the hash code is always same. Why is it like that?
Code:
public class BooleanClass {
public static void main(String[] args) {
Boolean b1 = new Boolean(true);
Boolean b2 = new Boolean(false);
Boolean b3 = new Boolean(true);
Boolean b4 = new Boolean(false);
Boolean b5 = new Boolean(false);
Boolean b6 = new Boolean(true);
System.out.println(b1.hashCode());
System.out.println(b2.hashCode());
System.out.println(b3.hashCode());
System.out.println(b4.hashCode());
System.out.println(b5.hashCode());
System.out.println(b6.hashCode());
}
}
Output:
1231
1237
1231
1237
1237
1231
Always the same numbers 1231
and 1237
are printed. Any reason?
Solution
The JavaDoc of Boolean.hashCode()
method says:
Returns the integer
1231
if this object representstrue
; returns the integer1237
if this object representsfalse
.
OTHER TIPS
The contract for hashCode() is:
If two objects are equal according to the
equals(Object)
method, then calling thehashCode()
method on each of the two objects must produce the same integer result.
And while boolean has only two values, true
and false
, you get only two different hash Codes.
Straight from Boolean class:
public int hashCode()
{
return ((this.value) ? 1231 : 1237);
}
This is the method who generate hash code for Boolean type. That's why you always get same hash code for true or false.
and this is constructor of Boolean
public Boolean(boolean paramBoolean)
{
this.value = paramBoolean;
}
so this.value will be either true or false if true it will give 1231 and if false it will give 1237
The point of a hashing function is to map data of arbitrary length to data of a fixed length. The values returned by a hash function are called hash values, hash codes, hash sums, checksums or simply hashes. A hash function will always return the exact same hash provided the input is the same, hence hashing true
will always equal 1231
and hashing false
will always equal 1237
If you really need to distinguish instances rather than values -- which is VERY rarely what you actually want, but it does occasionally happen -- see IdentityHashMap
.
(Essentially, IdentityHashMap
bypasses both the .equals()
and .hashcode()
implementations in the object's "real" class and uses those from Object
.)
I do not understand why the question raised so much attention. I would be surprised if it would be otherwise. As it was nicely pointed by others, it is even specified in documentation.
But even if there is no documentation, it is easy to understand the reason: You can just check the definition of a hash function function.
A hash function is any algorithm that maps data of arbitrary length to data of a fixed length.
And from mathematical definition map is a function, which means that the same values will always produce the same value.
If this can not help, you can just look at this example:
int a = 400;
int b = 400;
Should you expect that the hashes will be different? Most probably no. So why should they be different in case of true and false?