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 > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article focuses on "how to use the Integer cache pool in Java". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor learn how to use the Integer cache pool in Java.
Take a look at the following code first:
You can think about it first and try to write the answer and see if you can do it right.
Now I would like to announce the correct answer:
True
False
True
True
True
False
True
What? Why is the result like this? it's totally different from what I thought! That's what I'm going to talk about today: magical Integer!
It's not surprising at all, because there will be an Integer cache pool in Java, and the cache size is-128 to 127.
When using = =:
If you compare the Integer variable, the address value is compared by default.
Java's Integer maintains cache pools from-128 to 127
If one side of the comparison has an operation expression (for example, aqb), then the comparison is a specific value
When using equals ():
Equals () in both Integer and Long compares numeric values by default.
The equals () method of Long, the default implementation of JDK: determines whether it is a Long type
Pay attention to the problem of automatic unpacking and packing.
After JDK5.0, JVM instantiates nine object pools when it starts. These nine object pools are used to store wrapper classes of eight basic data types (such as Integer corresponding to int) and String objects (when we directly enclose a string in double quotes in the program, JVM goes to the object pool of String to see if there is an object with the same value, and if so, take the ready-made object. If not, create an object in the object pool and return it.
Take another look at the source code of the valueOf method:
Obviously, there is an instance called IntegerCache that caches the corresponding Integer. When calling valueof, it will first determine whether the corresponding cache object exists in the specified interval. If it is in the corresponding interval, the corresponding cache object will be returned directly.
In this way, you can naturally use = to make a judgment!
So I'm asking you now, why-128-127? Can you change it to another range?
The answer, of course, is yes, and JVM provides us with a parameter to modify it. You can set the-ea-Djava.lang.Integer.IntegerCache.high=1000 parameter.
At this point, I believe you have a deeper understanding of "how to use the Integer cache pool in Java". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.