[PATCH] Simplification proposal regarding TYPE-field initialization in primitive wrappers

Сергей Цыпанов sergei.tsypanov at yandex.ru
Tue Nov 26 19:29:06 UTC 2019


Hello,

while using java.lang.Integer.TYPE I've found out that currently wrapper classes for primitives initialize their own TYPE-fields by calling native method java.lang.Class.getPrimitiveClass().

This can be simplified by changing existing declaration (here for java.lang.Integer)

@SuppressWarnings("unchecked")
public static final Class<Integer>  TYPE = (Class<Integer>) Class.getPrimitiveClass("int");

to

public static final Class<Integer>  TYPE = int.class;

This is likely to improve start-up time as Class.getPrimitiveClass() is called 9 times (8 primitive type wrappers + java.lang.Void), after the change this method is not called at all and thus can be removed along with its backing C++ code on VM-side. The fields are initialized purely on Java side.

I've verified correctness of the substitution with this test run on JDK 11

@Test
void name() {
  assert void.class == Void.TYPE;
  assert boolean.class == Boolean.TYPE;
  assert byte.class == Byte.TYPE;
  assert char.class == Character.TYPE;
  assert short.class == Short.TYPE;
  assert int.class == Integer.TYPE;
  assert long.class == Long.TYPE;
  assert float.class == Float.TYPE;
  assert double.class == Double.TYPE;
}

The patch is attached.

Regards,
Sergey Tsypanov
-------------- next part --------------
A non-text attachment was scrubbed...
Name: primitive.patch
Type: text/x-diff
Size: 7814 bytes
Desc: not available
URL: <https://mail.openjdk.java.net/pipermail/core-libs-dev/attachments/20191126/17ba7c18/primitive.patch>


More information about the core-libs-dev mailing list