In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the example analysis of Java constant pool test questions, the article is very detailed, has a certain reference value, interested friends must read it!
Today, My partner asked me a Java question that gave him a headache and asked for the output result:
/ * @ author DreamSea 2011-11-19 * / public class IntegerTest {public static void main (String [] args) {objPoolTest ();} public static void objPoolTest () {Integer i1 = 40; Integer i2 = 40; Integer i3 = 0; Integer i4 = new Integer (40); Integer i5 = new Integer (40); Integer i6 = new Integer (0) System.out.println ("i1=i2\ t" + (i1 = = i2); System.out.println ("i1=i2+i3\ t" + (i1 = = i2+i3)); System.out.println ("i4=i5\ t" + (i4 = = i5)); System.out.println ("i4=i5+i6\ t" + (i4 = = i5+i6)); System.out.println ();}}
Output result:
I1=i2truei1=i2+i3 truei4=i5falsei4=i5+i6true
It looks like an Easy problem, but the Result output from Console is exactly the opposite of what we thought, so we wonder, why?
* through Internet search, we learned that Java provides the same object pooling mechanism as String classes to improve performance. Of course, the eight basic types of wrapper classes (Packaging Type) of Java also have an object pooling mechanism.
At compile time, Integer i1teachers 40th Java executes the encapsulation of the code into Integer i1=Integer.valueOf (40); by looking at the Source Code, you find that:
There is an inner class IntegerCache in Integer.valueOf () (similar to a constant array, also called object pool), which maintains an Integer array cache, with a Static Block (static block) in the Integer class with a length of (128 to 127) = 256.
As you can see from this static block, Integer has created Integer cache data with numeric values [- 128127] by default. So when using Integer i1 / 40, JVM will find a reference to that value directly in the object pool. That is, when an Integer object is declared in this way, JVM will first look for an object with a wood value of 40 in the cache pool of the Integer object, if there is a reference to the object directly; if not, create an object using New keyword and return the reference address of the object. Because [= =] in Java compares whether two objects are the same reference (that is, compare memory addresses), i2 and i2 are both referenced to the same object, and the So i1==i2 result is "true"; while i4=new Integer (40) and i5=new Integer (40) created by new, although their values are equal, will re-Create new Integer objects each time and will not be put into the object pool, so they are not the same reference and output false.
For i1==i2+i3 and i4==i5+i6, the result is True because the mathematical calculation of Java is operated on the memory stack, and Java will unpack i5 and i6. In fact, it compares the basic types (40-40-0), and their values are the same, so the result is True.
Well, I want to say that everyone here should have a better understanding of the Integer object pool. I asked in Nono what would happen if I changed 40 to 400.
I1=i2falsei1=i2+i3truei4=i5falsei4=i5+i6true
This is because their values are beyond the constant pool of Integer i1 and i2, and JVM creates new objects for i1 and i2 respectively (that is, Integer i1=new Integer (400)), so they are not the same reference.
The above is all the contents of the article "sample Analysis of Java constant Pool Test questions". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow 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.