In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Today Xiaobian to share with you about Java automatic boxing, automatic unpacking and Integer cache how to use the relevant knowledge points, detailed content, clear logic, I believe that most people still know too much about this knowledge, so share this article for your reference, I hope you can learn something after reading this article, let's take a look at it.
1. Preface
What is automatic packing and automatic unpacking? What is the Integer cache? What is the relationship between them?
Let's look at a title first.
Integer a = new Integer (1); Integer b = new Integer (1); System.out.println (astatableb); Integer c = 1bot Integer d = 1World system. Out.println (caterpillar d); Integer e = 128 System.out.println (eSystem.out.println)
Answer first, then look at the answer.
The answer is false true false, did you get it right?
Now that we have appeared together, let's list some knowledge points together.
two。 Packaging class
There are eight basic data types in Java, which can be divided into three categories:
Character type: char
Boolean: boolean
Numerical type: byte short int long float double
A wrapper class wraps eight basic data types into classes so that they can use the three major features of Java: encapsulation, inheritance, and polymorphism. The corresponding relationship is as follows:
Wrapper class byteByteshortShortintIntegerlongLongfloatFloatdoubleDoublebooleanBooleancharCharacter corresponding to the basic data type
The six wrapper classes corresponding to numeric types all inherit from the Number class.
3. Automatic packing and automatic unpacking
Eight basic data types correspond to eight wrapper classes, so how do they convert data?
/ / basic data types are subpackaged / / 1. The parametric structure Integer a = new Integer (1) / / 2. In fact, parameters constructed with parameters can also be strings, but to use the correct data, "123abc" cannot be converted to Integer type Integer b = new Integer ("123"); / / 3.valueOf () Integer c = Integer.valueOf (123); / / wrapper class to basic data type (xxxValue () float is floatValue () double is doubleValue ()) int d = a.intValue ()
The above forms are more cognitive, you can get an object through new or call a method, get a value and then call a property of the object.
There is no need to bother after Java 5.0. new features of auto-boxing and auto-unpacking have been added, which are actually very easy to understand.
Int a = 10 Integer b = a; / / automatic packing int c = b; / / automatic unpacking
At first glance, this form of object = value is not cognitive, but it can be achieved with the help of automatic boxing and automatic unpacking. Actually, the compiler is implemented with the help of valueOf () and xxxValue ().
Let's take a look at the valueOf () source.
/ * 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-128to 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 = 127;} private IntegerCache () {}}
As you can see, IntegerCache is a static inner class of Integer, and the IntegerCache.cache called by valueOf () is an array object. The size of the array depends on the maximum and minimum values in the range, and the default is [- 128127]. Of course, (annotated) this range can also be modified through JVM (which I don't understand). Then all the elements in the array are assigned an Integer object, and the cache is formed.
There is an array cache, which means that if the value is [- 128127], Integer objects created with valueOf () or autoboxing are taken out of the array, so the memory address that the object points to is exactly the same. If you use new or go beyond this range, you have to recreate the object.
Of course, not only Integer has caching mechanisms, but Byte, Short, Long, and Character all have caching mechanisms. Where the range of Byte,Short,Integer,Long is from-128to 127 and the range of the character is from 0 to 127.
5. Answer the questions: Integer a = new Integer (1); Integer b = new Integer (1); System.out.println (astatableb); Integer c = 1Tring Integer d = 1ten System.out.println (caterpillar d); Integer e = 128 System.out.println (eigenf)
The two objects created by 1.new point to different memory addresses even if the values are the same. The result is false when comparing with = =.
two。 Auto-boxing and caching mechanism, the two objects are actually the same, the return result is true
3. If the cache is out of scope, the new object will be new during execution. If the two objects are different, the returned result is false.
These are all the contents of the article "how to use Java Auto-boxing, Auto-unpacking and Integer caching". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
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.