In the third edition of the Java language specification, 5.1.7 boxing conversion,
If the value p being boxed is true, false, a byte, a char in the range/u0000 to/u007f, or an int or short number between-128 and 127, then let R1 and R2 be the results of any two boxing conversions of P. it is always the case that R1 = R2.
This is why the Java implementation that complies with the specifications must ensure that the integer cache must cover at least [-128,127.
I checked some information online. This value can be configured and specified. Configuration involves more advanced topics, which are not discussed here.
This content is also mentioned in "JVM pool ---- string, integer. Http://blog.csdn.net/cuser_online/archive/2010/11/16/6013582.aspx
Forgive me for being vernacular, as follows:
Integer a1 = 200;
Integer a2 = 200;
System. Out. println (a1 = a2); // false
Integer a3 = 100;
Integer A4 = 100;
// Integer ranges from-128 to 127 (flyweight (metadata mode), saving memory overhead)
System. Out. println (A3 = A4); // true
The following is an individual's guess based on the string pool mechanism (no specific information is found for the time being, as if I had seen similar information before ).
JVM also performs special processing on integer. It can be described as an integer whose nominal value is within the range of-128-127. If an integer instance is created in the form of a value assignment, it is directed to the corresponding integer instance in the constant pool.
In other words, the JVM opened up a space when it was started, and put all the integers with a nominal value range of-128-127 there. If an integer instance within this range is created in the future, you do not need to create an object in heap. Direct the object to the created object in the pool. Why? Of course, it is for cache and sharing.
In fact, this is another classic application of the metadata mode. How is this implemented? Generally, it is done by combining the factory and the multi-instance mode. In fact, you can change the singleton mode to the singleton mode. The above models are not described here.
Note: If the precedingCodeIs:
Integer a3 = new INTEGER (100 );
Integer A4 = new INTEGER (100 );
System. Out. println (A3 = A4) // false
Because the new instance creation method is specified, no matter what is shared, the object is directly created in the heap. Smart, you should understand the mechanism of the string class at once.