What is packing & unpacking?
The process of converting an int primitive type to an Integer wrapper type is called boxing, and vice versa is called unboxing.
First look at a piece of code
public static void main(String[] args) {
Integer a = 127, b = 127;
Integer c = 128, d= 128;
System.out.println(a == b); // true
System.out.println(c == d); // false
}
I don't know if there are still people who don't know the reason why this code appears true and false. From this we lead to the operation of Java boxing. We carry out the analysis with questions.
Boxing (valueOf())
public static Integer valueOf(int i) {
// -128 - 127
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
We can find that there is a judgment at the beginning. If the range of this value is between [-128, 127], then it is taken from this cache (Integer array), and if it is not in this range, then directly create a new one.
Why have a cache of [-128,127]?
I understand it, because in our business, there may be fields of Integer type such as various states and identifiers. These values are generally 0, 1, 2, 3 and the like, and they appear more frequently. If there is no Cache, then frequent new objects are required, and then released, which consumes a lot of memory space, so this cache appears, which can greatly help us optimize some space waste.
Why [-128,127]?
I took a look at this. I won't go into details here. It mainly depends on the basic knowledge of computers. After you understand what the original code, the inverse code, and the complement code are. It's easy to see why it is in this range.
This value can also be changed via startup parameters.
-XX:AutoBoxCacheMax=(size)
Performance issues with autoboxing
So now you should understand the reasons for the different results of the above code, have you ever thought about it, for example, in a for loop in our business, there is an operation similar to the statistical data, if there is automatic boxing, then there will be what is the problem? Let's look at the following piece of code.
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
Integer count = 0;
// int count = 0;
for (int i = 0; i < 5000000; i++) {
count += i;
}
System.out.println("计算时长:" + (System.currentTimeMillis() - startTime) + " ms");
}
// 执行结果:
// Integer 计算时长:51 ms
// int 计算时长:6 ms
Then, through the execution results, it can be clearly found that frequent new objects are automatically boxed and memory is allocated, resulting in performance loss in time and space.
short summary
Through the above source code reading and test analysis, we can draw the conclusion that we should try our best to avoid this type of conversion problem when we perform calculation statistics or method parameters. To improve the execution efficiency of our entire code.
unboxing(intValue)
There is no complicated logic for unboxing in general, and the basic type of the value is directly returned.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。