<i18n dev> RFR: 8342863: Use pattern matching for instanceof in equals methods of wrapper classes

Rémi Forax forax at openjdk.org
Thu Oct 24 05:44:11 UTC 2024


On Wed, 23 Oct 2024 15:36:08 GMT, Joe Darcy <darcy at openjdk.org> wrote:

>> src/java.base/share/classes/java/lang/Boolean.java line 259:
>> 
>>> 257:     public boolean equals(Object obj) {
>>> 258:         if (obj instanceof Boolean b) {
>>> 259:             return value == b.booleanValue();
>> 
>> I would go even a step further and use `value` instead of `booleanValue()`, `intValue()` etc because there is no need to call a method to get the value.
>> 
>> 
>>   if (obj instanceof Boolean b) {
>>             return value == b.value;
>
> Hi @forax,
> Yes, I had considered that before sending out the PR. It shouldn't matter from a performance perspective, so it would come to down to which version of the code was marginally easier to read.

Ah, too slow to answer ...
Anyway, the wrapper types tend to be used everywhere so there are also used in methods that are executed only a few times so only by the interpreter, in those case, accessing directly to the field is a win.

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

PR Review Comment: https://git.openjdk.org/jdk/pull/21652#discussion_r1814295461


More information about the i18n-dev mailing list