In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "how to use Integer IntegerCache". In daily operation, I believe many people have doubts about how to use Integer IntegerCache. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use Integer IntegerCache"! Next, please follow the editor to study!
Integer s = 1; Integer S1 = 1; System.out.println (s = = S1); Integer S2 = 128; Integer S3 = 128; System.out.println (S2 = = S3)
As a result, the question will be printed out as true, and the second answer to false; is what we normally understand. To understand why the first print is true, learn about = = and equals first
= =:
The basic type usually uses = = to compare the memory address, and if the memory address is the same, then the two values are naturally equal.
Equals:
The comparison of objects usually uses equals, while equals compares the contents of objects.
So
Integer s = 1 th Integer S1 = 1 x x System.out.println (s.equals (S1)); / / true
Let's go back to the initial question. The key lies in the inner class IntegerCache in Integer.
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. }} high = 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 () {}}
All the numbers in the range of-128127 are placed in the cache array, so find a range of = = and compare them to true.
So the advantage is that it must be much better to get it directly in the cache than to generate an object.
Byte Short Long all have similar practices here, and the study on "how to use Integer IntegerCache" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.