Packing type
The data type
There are eight base types and five reference types in Java.
Basic data types
Basic data types fall into three categories: character, Boolean, and numeric
- Char: 16
- Byte: stores a maximum of 255 bytes and ranges from -128 to 127.
- Short: 16 bits, the maximum data storage is 65536, and the data range is between -32768 and 32767.
- Int: 32-bit, the maximum data storage capacity is 2 ^ 32 minus 1, the data range is negative 2 ^ 31 to positive 2 ^ 31 minus 1.
- Long: 64-bit. The maximum data storage capacity is 2 ^ 64 minus 1. The data range is from negative 2 ^ 63 to positive 2 ^ 63 minus 1.
- Float: 32 bits. The value ranges from 3.4E-45 to 1.4e38. If the value is directly assigned, f or f must be added after the number.
- Double: 64-bit, ranging from 4.9E-324 to 1.8e308. The value can be assigned with or without D.
- Boolean: The value can be true or false.
For small, basic variables, storing them as objects is better than storing them directly on the stack as values
Reference data type
The five reference types are:
- class
- interface
- An array of
- The enumeration
- mark
The emergence of packaging types
Each base type has a corresponding wrapper type
Java is an object-oriented language, basic types do not have the nature of the object, other objects in order to “integration” is the packaging type (such as when we use the set type Collection will be sure to use the packing type rather than the basic types), it is equivalent to the basic types of “packaging”, it has the nature of the object, Properties and methods are added to enrich the operations of primitive types.
The difference between
Advantages of basic types: Relatively simple data storage and high computing efficiency Advantages of wrapper types: Provide more operations on data, such as collection elements must be object types, to meet the Java idea that everything is an object declaration is different, basic types do not apply to the new keyword, and wrapper types need to use the new keyword to allocate storage space in the heap; The basic type stores the value of the variable directly on the stack, while the wrapper type puts the object in the heap and uses it by reference. The initial value of the basic type is 0, and the initial value of the wrapper type is false. The initial value of the wrapper type is null
The split open a case using the
Packing is the process of basic type to corresponding packaging type, and corresponding unpacking is the reverse process.
Automatic unpacking and packing
Automatically converts to the corresponding wrapper class or primitive type when a wrapper class operation is used on a primitive type or when a wrapper class primitive type operation (such as operator) is performed on it. In order to achieve a shielded transformation process, externally only shown as the operation of variables.
The JDK source
We use the newValueOf and xxxValue() methods in the wrapper class, which can also be called manually by ourselves, as a syntactic sugar.
Caching mechanisms
Take the Integer class as an example. When creating a wrapper object, the packing mechanism checks whether the value is in the range of -128 to -127. If the condition is met, the packing mechanism searches the cache (constant pool) for the specified value.
The corresponding JDK source code is IntegerCache
private static class IntegerCache { static final int low = -128; static final int high; static final Integer cache[]; // Declare final, so cached objects are put into the constant pool; Static {// High value may be configured by property int h = 127; static {// High value may be configured by property int h = 127; String integerCacheHighPropValue = sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high"); if (integerCacheHighPropValue ! = null) { try { int i = parseInt(integerCacheHighPropValue); i = Math.max(i, 127); // Maximum array size is Integer.MAX_VALUE h = Math.min(i, Integer.MAX_VALUE - (-low) -1); } catch( NumberFormatException nfe) { // If the property cannot be parsed into an int, ignore it. } } high = h; cache = new Integer[(high - low) + 1]; int j = low; for(int k = 0; k < cache.length; k++) cache[k] = new Integer(j++); // range [-128, 127] must be interned (JLS7 5.1.7) Assert IntegerCache. High >= 127; } private IntegerCache() {} }Copy the code
When we define two integers in the range of [-128 — +127] and have the same value, we use == to compare the value to true.
If the value is greater than 127 or less than -128, an integer will be new, even if the two values are the same
Enjoy the meta mode, take the common variable object
The five wrapper classes of Integer, Byte, Short, Long, and Character all have buffering mechanisms, and the default buffering values are -128 to 127
Float,Double, and Boolean have no buffering.
Design of caching mechanism
IntegerCache is an inner class of Integer that instantiates objects between -128 and high
The lower limit is fixed, but the upper limit can be adjusted by setting the JDK’s AutoBoxCacheMax parameter to [-128,N];
IntegerCache is not instantiated, it is private static class IntegerCache, and Integer uses its static method directly