What are automatic boxing and automatic unboxing? What is Integer cache? What's the relationship between them?
Let’s look at a question first.
Integer a = new Integer(1); Integer b = new Integer(1); System.out.println(a==b); Integer c = 1; Integer d = 1; System.out.println(c==d); Integer e = 128; Integer f = 128; System.out.println(e==f);
Answer first, look at the answer later.
The answer is false true false, are you correct?
Now that one piece has appeared, let’s share the knowledge points together
There are eight basic data types in Java, which can be divided into three categories:
Character type: char
Boolean type: boolean
Numeric type: byte short int long float double
Packaging classes wrap eight basic data types into classes so that they can use Java's three major features: encapsulation, inheritance, and polymorphism. The corresponding relationship is as follows:
Basic data type | Corresponding packaging class |
---|---|
byte | Byte |
short | Short |
int | Integer |
#long | Long |
float | #Float |
double | Double |
boolean | Boolean |
char | Character |
The six packaging classes corresponding to numerical types all inherit from the Number class.
The eight basic data types correspond to the eight packaging classes, so how do they perform data conversion?
//基本数据类型转包装类 //1.有参构造 Integer a = new Integer(1); //2.实际上,有参构造的参数也可以是字符串,不过要使用正确的数据,“123abc”不可能会转换为Integer类型 Integer b = new Integer("123"); //3.valueOf() Integer c = Integer.valueOf(123); //包装类转基本数据类型(xxxValue() float是floatValue() double是doubleValue()) int d = a.intValue();
The above forms are relatively consistent with cognition. To obtain an object, you can use new or call a certain method. To obtain a value, call a certain attribute of the object.
After Java 5.0, you don’t have to be so troublesome. New features of automatic boxing and automatic unboxing have been added. In fact, the two concepts are very easy to understand.
int a = 10; Integer b = a; //自动装箱 int c = b; //自动拆箱
At first glance, the form of object=numeric value does not conform to cognition, but it can be achieved with the help of automatic boxing and automatic unboxing. In fact, the compiler still implements it with the help of valueOf() and xxxValue().
Let’s take a look at the valueOf() source code.
/** * Returns an {@code Integer} instance representing the specified * {@code int} value. If a new {@code Integer} instance is not * required, this method should generally be used in preference to * the constructor {@link #Integer(int)}, as this method is likely * to yield significantly better space and time performance by * caching frequently requested values. * * This method will always cache values in the range -128 to 127, * inclusive, and may cache other values outside of this range. * * @param i an {@code int} value. * @return an {@code Integer} instance representing {@code i}. * @since 1.5 */ public static Integer valueOf(int i) { if (i >= IntegerCache.low && i <= IntegerCache.high) return IntegerCache.cache[i + (-IntegerCache.low)]; return new Integer(i); }
valueOf() does not simply return an Integer object, but first makes a judgment. If the input data matches a certain range, it will return a specific object. From the comments, this range The default is [-128,127], and may be a larger range; beyond this range, a new object will be returned. The IntegerCache data used is the cache of Integer.
Numerical calculations are used frequently in daily life. If you keep getting new Integer objects, the overhead will be very large. Therefore, Java will automatically generate a new Integer object when executing the program. Static arrays are used as caches. The default cache array range corresponding to Integer is [-128,127]. As long as the data is within this range, the corresponding object can be obtained from the cache.
Look at the IntegerCache source code.
/** * Cache to support the object identity semantics of autoboxing for values between * -128 and 127 (inclusive) as required by JLS. * * The cache is initialized on first usage. The size of the cache * may be controlled by the {@code -XX:AutoBoxCacheMax=<size>} option. * During VM initialization, java.lang.Integer.IntegerCache.high property * may be set and saved in the private system properties in the * sun.misc.VM class. */ private static class IntegerCache { static final int low = -128; static final int high; static final Integer cache[]; 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() {} }
As you can see, IntegerCache is a static internal class of Integer. IntegerCache.cache called by valueOf() is an array object. The size of the array depends on the maximum and minimum values in the range. The default is [- 128, 127], of course (the comment says) this range can also be modified through the JVM (I don't understand this). Then each element in the array will be assigned an Integer object, and the cache will be formed.
There is an array cache, which means that if the value is in [-128,127], the Integer object created using valueOf() or automatic boxing is taken out from the array, so the memory address pointed to by the object is exactly the same. If you use new or exceed this range, the object must be re-created.
Of course, not only Integer has a caching mechanism, Byte, Short, Long, and Character all have caching mechanisms. The range of Byte, Short, Integer and Long is -128 to 127, and the range of Character is 0 to 127.
Integer a = new Integer(1); Integer b = new Integer(1); System.out.println(a==b); Integer c = 1; Integer d = 1; System.out.println(c==d); Integer e = 128; Integer f = 128; System.out.println(e==f);
1. Even if the two objects created by new have the same value, they point to different memory addresses. Using == for comparison returns a false
2. Automatic boxing and caching mechanism. The two objects are actually the same and the return result is true
3. Beyond the cache range, a new object will be new during execution. If the two objects are different, return The result is false
The above is the detailed content of How to use Java automatic boxing, automatic unboxing and Integer caching. For more information, please follow other related articles on the PHP Chinese website!