<i18n dev> RFR: 8332826: Make hashCode methods in ArraysSupport friendlier [v2]

Pavel Rappo prappo at openjdk.org
Wed May 29 15:54:02 UTC 2024


On Wed, 29 May 2024 12:53:42 GMT, Pavel Rappo <prappo at openjdk.org> wrote:

>> src/java.base/share/classes/jdk/internal/util/ArraysSupport.java line 252:
>> 
>>> 250:         return switch (length) {
>>> 251:             case 0 -> initialValue;
>>> 252:             case 1 -> 31 * initialValue + (int) a[fromIndex];
>> 
>> Suggestion:
>> 
>>             case 1 -> 31 * initialValue + (int) a[fromIndex]; // sign extension
>
> To be honest, I don't think that this cast is needed. A better solution than to add a comment would be to delete all `(int)` casts from new `hashCode*` methods of `ArraysSupport`.
> 
> Those `(int)` casts migrated from `hashCode` methods of `Arrays` where there were used if neither of two `+` operands were of type `int`. But in `ArraysSupport` it's no longer the case: `31 * initialValue` is always `int` because `initialValue` is. So, `a[fromIndex]` is promoted to `int` by the virtue of https://docs.oracle.com/javase/specs/jls/se22/html/jls-5.html#jls-5.6.
> 
> For more confidence, consider that the `private static int hashCode` methods (implementation) of `ArraysSupport` do not have those casts.

Removed casts in https://github.com/openjdk/jdk/pull/19414/commits/d8dabc68c4264520fd6c57c949049f2ab5c8e0ec.

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/19414#discussion_r1619127224


More information about the i18n-dev mailing list