There are eight primitive data types in Java (byte, short, char, int, float, double, long), and int is one of them
Intger is the wrapper class corresponding to int. It has a field of type int to store data and provides automatic packing and unpacking functions. Automatic packing and automatic unpacking are realized by static factory methods Valueof and intValue respectively.
Integer IntegerCache provides the cache for values between -128 and 127. As a matter of practice, most data operations are concentrated in this scope. The Valueof method uses caching.
Caching is not unique to Integer, for example:
- Boolean, caches instances of true/false. Specifically, only two instances of constants are returned. Boolean.TRUE/FLASE
- Short, also caches values between -129 and 127
- Byte, numeric mailbox, so it’s all cached
- Character, cache range ‘\u0000’ to ‘\u007F’.
Like String, the int field in Integer is declared private final. This design makes some sense. Integer provides a getInteger() method for reading system properties easily, and Integer cannot be modified in this scenario.
It is worth noting that the primitive int is not thread-safe. In particular, for wide data types such as float and double, the atomicity of the update operation is not guaranteed. It is possible for programs to read values with only half of the data bits updated.
Integer Specifies the memory structure and size of the object:
- Mark Word: Mark bit 4 bytes, similar to lightweight lock bit, biased lock bit, etc.
- Class object pointer :4 bytes pointing to the memory address of the corresponding Class object.
- Object actual data: all member variables of the object.
- Align: Aligns the fill bytes to eight bytes.
Integer Specifies the occupied memory size. The value is 4+4+4+4=16 bytes.