How Java basic data types are represented in a constant pool
Amount
The use of basic data types is very large, Java constant pool, in memory savings is a good mechanism, the same data, in a constant pool as long as a copy can be.
8 Basic types of Java (Byte, short, Integer, Long, Character, Boolean, float, double), except for Float and double, all six of them implement a constant pool. However, they use constant pools only when they are greater than or equal to 128 and are less than or equal to 127.
As can be seen from the following example:
The results are as follows:
Equals 127:
True
*****************
equals 128:
False
*****************
Equals-128:
True
*****************
Equals-129:
False
*****************
Test Boolean
True
False
When we assign a value to an integer, we actually call the integer.valueof (int) method to see the source code, which is implemented as follows:
And the Integercache implementation is as follows:
Note that the cache array is static.