Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Why in Java 1000 is false and 100 is true?

2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "why 1000 is false and 100 is true" in Java. Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

If you run the following code

Integer a = 1000, b = 1000; System.out.println (a = = b); / / 1 Integer c = 100, d = 100; System.out.println (c = = d); / / 2

You'll get it.

False

True

Basics: we know that if two references point to the same object, use = to indicate that they are equal. If two references point to different objects, use = = to indicate that they are not equal, even if their contents are the same.

Therefore, the latter statement should also be false.

That's what's interesting about it. If you look at the Integer.java class, you will find that there is an internal private class, IntegerCache.java, which caches all integer objects from-128to 127s.

So it turns out that all the small integers are cached internally, and then when we declare something like--

Integer c = 100

What it actually does internally is

Integer I = Integer.valueOf

Now, if we look at the valueOf () method, we can see

Public static Integer valueOf (int I) {if (I > = IntegerCache.low & & I return IntegerCache.cache [I + (- IntegerCache.low)]; return new Integer (I);}

If the value ranges from-128 to 127, it returns the instance from the cache.

So...

Integer c = 100, d = 100

Points to the same object.

That's why we write

System.out.println (c = = d)

We can get true.

Now you might ask, why do you need caching here?

The logical reason is that the usage of "small" integers in this range is higher than that of large integers, so it is valuable to use the same underlying objects and can reduce potential memory footprint.

However, you can misuse this feature by reflecting API.

Run the following code and enjoy its charm

Public static void main (String [] args) throws NoSuchFieldException, IllegalAccessException {Class cache = Integer.class.getDeclaredClasses () [0]; / 1 Field myCache = cache.getDeclaredField ("cache"); / / 2 myCache.setAccessible (true); / / 3 Integer [] newCache = (Integer []) myCache.get (cache); / / 4 newCache [132] = newCache [133]; / / 5 int a = 2; int b = a + a System.out.printf ("% d +% d =% d", a, a, b); / /} "Why 1000 is false and 100 is true" in Java. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report