java.util.Objects, round two

Joseph D. Darcy Joe.Darcy at Sun.COM
Wed Oct 14 17:14:18 UTC 2009


Paul Benedict wrote:
> Joe,
>
> I am confused over this warning:
>   
>> * <b>Warning: When a single object reference is supplied, the returned
>> * value does not equal the hash code of that object reference.</b> This
>> * value can be computed by calling {@link #hashCode(Object)}.
>>     
>
> I first looked into Arrays#deepHashCode and did not see any companion
> warning there; therefore I am presuming this is new functionality. 

No, the behavior is the same.  Objects.hash is just a var-arg wrapper 
around Arrays.hashCode(Object[]).  The semantic difference is that for 
Object.hash, one is passing a number of objects, which happen to get 
bundled into an array, whereas in Arrays.hashCode an array is being 
explicitly passed.

> Two
> questions remain based on looking at the javadoc alone and not knowing
> its implementation:
>
> For when a single object reference is supplied:
> 1) Why is the hash code not the hash code of the single object? Is
> there harm in doing so?
>   

As implied by the chain of javadocs, for a single object, hashcode + 31 
is returned via the List.hashCode algorithm on a single element list.

-Joe



More information about the core-libs-dev mailing list