In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to understand Java packaging class". In daily operation, I believe many people have doubts about how to understand Java packaging class. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to understand Java packaging class"! Next, please follow the editor to study!
I. introduction of packaging class
1. Why do you need a wrapper class?
We know that Java is an object-oriented programming language, but the basic data types in Java are not object-oriented, but in practice, we often need to convert basic data types into objects to facilitate operation, for example, in the operation of collections, we need to convert basic type data into objects, so wrapper classes appear.
two。 What is the packaging class?
The wrapper class, as its name implies, is the class that wraps what and what is wrapped. Obviously, this is the class that wraps the basic types. The function of the wrapper class is to convert the basic type into an object and treat the basic type as an object.
As we know in Java, there are eight basic data types, so the corresponding wrapper class is also eight, and the wrapper class is the initial capitalization of the base type name. With the exception of Integer and Character, they display the full name, as shown in the following table:
Basic data types correspond to wrapper classes
ByteByteshortShortintIntegerlongLongfloatFloatdoubleDoublecharCharacterbooleanBoolean
Second, the inheritance relationship of the packaging class
By reading the official API documentation of Java8 or looking at the source code, we can see that the inheritance relationships of the eight wrapper classes are as follows:
From the inheritance diagram above, we can actually remember that six numbers related to the wrapper class inherit from the Number class, while the other two that are not related to numbers inherit the Object class by default. By looking at the official API documentation, we can also see that these eight wrapper classes all implement Serializable and Comparable interfaces. For example, the Integer class in the following figure
Public final class Integer extends Number implements Comparable {}
Third, the use of packaging class (basic operation)
Next on the wrapper class I will talk about Integer wrapper class, and so on, the usage and operation are similar, but the name is not the same.
1. Construction method of wrapper class
All eight wrapper classes have constructors with their own corresponding type parameters, of which eight wrapper classes have constructor overloads in addition to Character, and the parameters are of type String.
Integer one = new Integer; Integer two = new Integer ("666")
two。 Automatic unpacking of packaging class
Before we know about automatic unpacking, we need to know what unpacking and packing are. In fact, unpacking mainly deals with the problem of mutual conversion between basic types and packaging types.
Boxing: the process of converting a basic type to a wrapper type is called boxing.
Unpacking: the process of converting a packaging type to a basic type is called unpacking.
In fact, before the JDK1.5 version, there is no automatic unpacking, developers have to manually unpack and unpack:
/ / manually boxing, that is, converting the basic type 10 to the reference type Integer integer = new Integer (10); / / or Integer integer1 = Integer.valueOf (10); / / unboxing manually, that is, converting the reference type to the basic type int num = integer.intValue ()
After the JDK1.5 version, in order to reduce the work of developers, the functions of automatic boxing and automatic unpacking are provided. Automatic unpacking and automatic boxing are implemented, as shown in the following code:
/ / Auto-packing Integer one = 1; / / automatic unpacking int two = one + 10
In fact, the above two ways are essentially the same, but one is realized automatically and the other is realized manually. As for how to realize automatic unpacking, I will not do any in-depth research here.
Fourth, the caching mechanism of wrapper class
Let's first look at the following code, example 1:
Public static void main (String [] args) {Integer i1 = 100; Integer i2 = 100; Integer i3 = new Integer (100); Integer i4 = new Integer (100); System.out.println (i1 = = i2); / / true System.out.println (i1 = = i3); / / false System.out.println (i3 = = i4); / / false System.out.println (i1.equals (i2)); / / true System.out.println (i1.equals (i3)) / / true System.out.println (i3.equals (i4)); / / true}
When we change the value to 200, example 2:
Public static void main (String [] args) {Integer i1 = 200; Integer i2 = 200; Integer i3 = new Integer; Integer i4 = new Integer (200); System.out.println (i1 = = i2); / / false System.out.println (i1 = = i3); / / false System.out.println (i3 = = i4); / / false System.out.println (i1.equals (i2)); / / true System.out.println (i1.equals (i3)) / / true System.out.println (i3.equals (i4)); / / true}
Through the above two ends of the code, we find that the value has been modified, and the execution result of the fifth line of code has changed. Why? First of all, we need to make it clear that the first and second lines of code actually implement the process of automatic boxing, that is, the automatic implementation of the Integer.valueOf method, and secondly, = = compares addresses, while equals compares values (here the eauals is rewritten, so the comparison is specific values), so obviously there is no doubt about the execution results of the last five lines of code. Since = = is comparing addresses, why is the fifth line of code in example 1 true? we need to understand the caching mechanism of the wrapper class.
In fact, if we look at the source code of the Integer class, we can see that there is a private static inner class at line 780, as follows:
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 (I127); / / 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. }} hhigh = 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 () {}}
We know that the static inner class is loaded when the entire Integer is loaded. The above code initializes an array of type Integer called cache, with a value range of [- 128,127]. The function of the cache mechanism is to instantiate the wrapper class objects with the corresponding range values in advance, and as long as the objects in the cache scope are created, the objects that have been instantiated are used. Thus, the repeated creation of multiple identical packaging class objects is avoided, and the use efficiency is improved. If the scope of the object we use is within [- 128,127], go directly to the static area to find the corresponding object. If the scope of the object used exceeds this range, it will help us to create a new Integer object. In fact, the following source code means:
Public static Integer valueOf (int I) {if (I > = IntegerCache.low & & I)
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.