Today I did a topic as follows:
Integer A=10;integer b=10; System.out.print (a==b); Integer C=200;integer d=200; System.out.print (C==d);
Please say the output:
The answer is: true,false
Isn't it weird?
Turn the source to haha;
View Integer.value (int i) method
public static Integer valueOf (int i) {if (i >= integercache.low && i <= integercache.high)//judged if greater than low or Less than high return integercache.cache[i + (-integercache.low)];//out of this cache returns the return new Integer (i); }
View Internal Classes
private static class Integercache {static final int low =-128; static final int high; Static final Integer cache[]; static {//high value is configured by property int h = 127; String Integercachehighpropvalue = Sun.misc.VM.getSavedProperty ("Java.lang.Integer.IntegerCache.high");//Get JVM parameter if (integercachehighpropvalue! = null) {try {int i = parseint (integer Cachehighpropvalue); i = Math.max (i, 127);//Take both maximum//Maximum array size is integer.max_value h = math.mi N (I, Integer.max_value-(-low)-1);//fear that the virtual machine array is too large to determine the minimum value of the two} catch (NumberFormatException nfe) { If The property cannot is parsed into an int, ignore it. }} high = h; cache = new Integer[(high-low) + 1];//Create array int j = low; for (int k = 0;K < Cache.length; k++) Cache[k] = new Integer (j + +);//First step to create this object array//range [ -128, 127] must be interned (JLS7 5 .1.7) Assert Integercache.high >= 127;//debug assert} private Integercache () {}}
You can see that you can set this maximum or minimum buffer by setting the Java.lang.Integer.IntegerCache.high of the JVM's parameters, but it will cause the size of the cache array of this Integer to become larger
When the parameter is not set, when the integer is between 128 and 127, the object is removed from the cache array, so the comparison is the same.
The above-mentioned questions can also be explained.
Java integer type Comparison