JDK-8317277 - Java language implementation of value classes and objects

David Alayachew davidalayachew at gmail.com
Wed Oct 18 23:21:13 UTC 2023


To respond to your actual point though, I think it would be great for
identity classes to get this too.

On Wed, Oct 18, 2023 at 7:18 PM David Alayachew <davidalayachew at gmail.com>
wrote:

> Potentially silly question in regards to the identityHashCode.
>
> Is it possible for an identityHashCode to produce a negative value?
>
> I tried the following, and it is still running. I don't have any window
> functions though, so I can't make arbitrary strings, just one character
> ones.
>
> $ jshell
> |  Welcome to JShell -- Version 21
> |  For an introduction type: /help intro
>
> jshell> new java.util.Random()
>    ...> .ints()
>    ...> .filter(Character::isValidCodePoint)
>    ...> .mapToObj(Character::toChars)
>    ...> .map(String::new)
>    ...> .mapToInt(System::identityHashCode)
>    ...> .filter(a -> a < 0)
>    ...> .findFirst()
>
> If the answer is, no negatives, then I think this would be best. I don't
> know if it would be "all value objects are -1", or something else.
>
>
> On Wed, Oct 18, 2023 at 5:35 PM Archie Cobbs <archie.cobbs at gmail.com>
> wrote:
>
>> On Wed, Oct 18, 2023 at 3:59 PM Remi Forax <forax at univ-mlv.fr> wrote:
>>
>>>
>>> 1. If v is a value object, what does System.identityHashCode(v) return?
>>>
>>>
>>> A combination of some field values of the object.
>>> It has to be fast so the VM should not use all fields if there are a lot
>>> of them or if some values are value class (or field type that can
>>> potentially store value class) because it will make the computation
>>> recursive.
>>>
>>
>> Hmm, that doesn't seem right.
>>
>> Isn't it hashCode()'s job to produce a value based on the object's
>> fields? The whole point of System.identityHashCode() is to NOT do that,
>> at least as of today.
>>
>> I'd expect System.identityHashCode() to return 0 for all value classes
>> or throw an exception.
>>
>> After all, value classes are not supposed to have an identity, right?
>>
>>
>>> 2. If V is a value class, is {Weak,Soft,Phantom}Reference<V> supported?
>>>
>>>
>>> Nope. There is maybe no address/no pointer so no weak reference.
>>>
>>
>> OK. How is this going to be enforced?
>>
>> -Archie
>>
>> --
>> Archie L. Cobbs
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/valhalla-dev/attachments/20231018/ceba96b2/attachment.htm>


More information about the valhalla-dev mailing list