JAVA Integer value range, javainteger Value
Source: http://hi.baidu.com/eduask%C9%BD%C8%AA/blog/item/227bf4d81c71ebf538012f53.html
Package com. test; public class Test {public static void main (String [] args) {Integer a = 100; // if new is used, the = value must be false Integer B = 100; system. out. println (a = B); // true Integer c = 150; Integer d = 150; System. out. println (c = d); // false }}
Why?
1. During java compilation, Integer a = 100; is translated as-> Integer a = Integer. valueOf (100 );
2. The comparison is still the comparison of objects.
3. In the jdk source code
Public static Integer valueOf (int I) {final int offset = 128; if (I >=- 128 & I <= 127) {// must cache return IntegerCache. cache [I + offset]; // when the value range is met, enter the static IntergerCache that is also created, the value of I + offset indicates to retrieve the value of the lower mark in the cache array} return new Integer (I); // when the value range is-128 and 127 is not met. Remember to use: new, open up new memory space, not in the IntergerCache management area}
While
Private static class IntegerCache {private IntegerCache () {} static final Integer cache [] = new Integer [-(-128) + 127 + 1]; // open-128 to 127 of the memory zone. There are 0 locations. Oh, static {for (int I = 0; I <cache. length; I ++) cache [I] = new Integer (I-128); // assign values to each object in the memory array }}
To improve efficiency, java initializes an integer object between-128--127, so the value assignment is within the same range.
Add one more sentence
Integer a = 100;
A ++;
// Here, a ++ creates a new object, not a previous object.
Public static void main (String [] args) {Integer a = 100; Integer B = a; // at this time, the B Pointer Points to the heap address with a value of 100, that is, the heap address of, a = B sets up a ++; // at this time, the value pointed to by a changes to 101, and the pointer points to the heap address of 101. B then points to 100 System. out. println (a = B); // false}