In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about the operation of JDK1.5 sealing and unpacking function. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
We know that all types in Java are either reference types and primitive data types, where the original data type has its corresponding package class (Wrapper Class), including: Byte (byte), Short (short), Integer (int), Long (long), Float (float), Double (double), Boolean (bool), Char (char).
We refer to the conversion between primitive data types and their package classes as sealing and unpacking operations.
Generics provides us with the ability to automatically unpack the box. Remember that when setting the type parameter of a generic type (type parameter), you can only use reference types, never the original data types. Let's use the following example to illustrate:
Example1.2.1
Public int sum (List ints) {ints = 0; for (int n: ints) {s + = n;} return s;}
In this example, we see that unboxing occurs when ints binds the value to n while traversing.
Example1.2.2
Public Integer sumInteger (List ints) {Integer s = 0; for (Integer n: ints) {s + = n;} return s;}
Here we see that the unpacking and sealing operations have been done repeatedly during the step s + = n. We can see that although we are doing the same thing, the efficiency difference in JVM is about 60%.
When discussing sealing and unpacking, we should pay attention to the following two problems:
(1) the definition of the = sign is different between the original and the reference type. when dealing with the problem of equality, we should pay special attention to the problems caused by sealing and unpacking:
Example1.2.3
List bigger = new ArrayList (200,300,400); Assert sumInteger (bigger) = = sum (bigger); Assert sumInteger (bigger)! = sumInteger (bigger) / / not recommended
We see that when comparing the * paragraph, the Integer is unboxed, and then compared with int, the result must be 900. 0. The second comparison is that because both are Integer, but they are not references to the same object, they are naturally not equal.
We recommend using the = = sign to compare int types and using the equals method to compare the equality between Integer types.
(2) when byte, boolean,int, short, char (\ u0000 -\ u007f) must be cached when sealing the box, and other values are also allowed to be cached.
Example1.2.4
List smalls = new List; Assert sumInteger (smaller) = = sum (smaller); Assert sumInteger (smaller) = = sumInteger (smaller) / / not recommended
Because the total value is 6, the value after sealing is cached, and the next time the value is also 6, the value in the cache will be called, so we see that the second one is equal. In general, it is not specified that the same value will be returned to the same or different objects twice. Instead of using the = = sign to compare referenced objects, we use the equals method to determine the equality between objects.
After reading the above, do you have any further understanding of the problems that should be paid attention to in the operation of JDK1.5 sealing and unpacking function? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.