In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
It is believed that many inexperienced people don't know what to do about how to implement 2-2-5 in the integer type of Java. Therefore, this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
Let's take a look at this magical Java code:
Public static void main (String [] args) throws Exception {doSomethingMagic (); System.out.printf ("2 + 2 =% d", 2 + 2);
Execution result: 2 + 2 = 5
So what amazing things did doSomethingMagic do? Look at the code first:
Private static void doSomethingMagic () throws Exception {Class cache = Integer.class.getDeclaredClasses () [0]; Field c = cache.getDeclaredField ("cache"); c.setAccessible (true); Integer [] array = (Integer []) c.get (cache); array [132] = array [133];}
So this example actually contains a knowledge point about the integer type Integer in Java.
Some friends may be a little confused about the code in doSomethingMagic. Let's take a look at the 2 + 2 decompiled code in line 17 above:
The editor calculates the value of 2 + 2 first, which is equal to 4. The final value printed by System.out.println is actually the return value of Integer.valueOf (4).
So let's take a look at the implementation of Integer.valueOf in JDK:
The above implementation code, from lines 830 to 832, is logically clear: if the parameter I of valueOf is between IntegerCache.low and IntegerCache.high, that is, the closed interval of [- 128,127], it is returned directly from the cache area of IntegerCache. Code 832 is executed only if the input parameter I is not in the [- 128127] range, creating a new Integer instance based on the input parameter I.
With this idea, we can see doSomethingMagic much more clearly. This method sets the member cache of the above IntegerCache to be accessible through Java reflection: setAccessible (true), and then overrides the value of element 132 of IntegerCache with the value of element 133.
We found in the Eclipse debugger that the 132nd element in Integer cache has a value of 4 and the 133rd element has a value of 5. The original Integer.valueOf method, for input 4, returns the value of the 132nd element, 4, from Integer cache. Now the value of this element is overwritten by the 133rd element, 5, so we end up with 2 + 2 = 5.
Summarize the scenario in one sentence: 2 + 2 = 4 = Integer.valueOf (4) = 5 (because the corresponding record of 4 in Integer cache has been explicitly replaced with 5 by our code).
After reading the above, have you mastered how to achieve 2-2-5 in the integer type of Java? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
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
Using 5why Analysis method to analyze the problem-zookeeper
© 2024 shulou.com SLNews company. All rights reserved.