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/01 Report--
This article mainly introduces the relevant knowledge of "how to apply Java packaging class". The editor shows you the operation process through an actual case. The operation method is simple and fast, and it is practical. I hope this article "how to apply Java packaging class" can help you solve the problem.
I. Overview of Packaging Class
Java has eight basic data types: integer (byte, short, int, long), floating point (float, double), Boolean boolean, character char. Accordingly, Java provides eight packaging classes: Byte, Short, Integer, Long, Float, Double, Boolean, Character. Wrapper classes create objects in the same way as other classes.
Integer num = new Integer (0); / / create an Integer object with a value of 0. 2. Automatic packing and unpacking mechanism of packaging class
The above construction object statement is actually a conversion from a basic data type to a wrapper class. In applications, we often need to convert between basic type data and wrapper class objects.
Integer num1 = new Integer (1); / / change the basic data type to the wrapper class int num2 = num1.intValue (); / / change the wrapper type to the basic data type System.out.println (num1 + "" + num2)
Java provides us with automatic packing and unpacking mechanism for convenience and other purposes such as performance tuning. This mechanism simplifies the conversion of basic types and packaging types.
/ 1. Automatic packing and unpacking mechanism in packaging class Integer num1 = 1; / automatic packing int num2 = num1; / / automatic unpacking System.out.println (num1 + "" + num2)
When you decompile the above code using the jad tool, the result is as follows.
Integer integer = Integer.valueOf (1); int I = integer.intValue (); System.out.println ((new StringBuilder ()) .append (integer) .append ("\ t") .append (I) .toString ())
It can be seen that the Java compiler helps us complete the conversion operation. In addition, we can see that in addition to using the new keyword, you can also use the valueOf () method of the Integer class to create an Integer object. There is a difference between these two ways, which we will talk about below.
Third, the cache mechanism in the wrapper class
As mentioned earlier, there are two ways to create wrapper class objects: the new keyword and the valueOf () method. Let's look at a piece of code and feel the difference.
/ / 2. The caching mechanism in the wrapper class: Integer num3= 10 + Integer num4 = 10 num4 Integer num5= new Integer (20); Integer num6 = new Integer (20); Integer num7= 128 num4 Integer num8 = 128 num4 System.out.println ((num3==num4) + "+ num3.equals (num6)); System.out.println ((num5==num6) +" + num5.equals (num6)); System.out.println (num7==num8) + "" + num7.equals (num8))
The running result is
Let's take a look at its decompiled code.
Integer integer = Integer.valueOf (10); Integer integer1 = Integer.valueOf (10); Integer integer2 = new Integer (20); Integer integer3 = new Integer (20); Integer integer4 = Integer.valueOf (128); Integer integer5 = Integer.valueOf (128); System.out.println ((new StringBuilder ()) .append (integer = = integer1) .append ("\ t") .append (integer.equals (integer1). ToString ()) System.out.println ((new StringBuilder () .append (integer2 = = integer3) .append ("\ t") .append (integer2.equals (integer3)) .toString (); System.out.println ((new StringBuilder ()) .append (integer4 = = integer5) .append ("\ t") .append (integer4.equals (integer5)) .toString ())
First, let's look at the source code of the valueOf () method of Integer
Public static Integer valueOf (int I) {if (I > = IntegerCache.low & & I = 127;}
You can see that whenever the Integer class is used for the first time, the static inner class of Integer is loaded, creating Integer objects of-128to127when loaded, and an array cache to cache these objects. When an object is created using the valueOf () method, the cached object is returned directly, that is, no new object is created; when you use the new keyword or to create a value object that is less than-128and greater than 127using the valueOf () method, a new object is created.
/ / 2. The caching mechanism in the wrapper class Integer num3 = 10 int num4 = 10 x Integer num5 = new Integer (20); Integer num6 = new Integer (20); Integer num7 = 128 th Integer num8 = 128
Since num3 and num4 all point to the same cached Integer object, the result of comparison with = = is that true;num5 and num6 use the new keyword to point to two different new objects, and the result is that false;num7 and num8 are boxed automatically, but when the valueOf () method is executed, the condition I > = IntegerCache.low & & I is not satisfied.
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.