Integer is a simple one, but it's a little different. Integer is a wrapper class of int, which can play the role of caching. In the java foundation, it said that its range is (- 128-127). In this return, there is a cache, and no new integer object will be created, and its maximum value can be set by setting VM parameters.
Let's take a look at the source code:
public static Integer valueOf(int i) { if(i >= -128 && i <= IntegerCache.high) return IntegerCache.cache[i + 128]; else return new Integer(i); }
private static class IntegerCache { static final int high; static final Integer cache[]; static { final int low = -128; // high value may be configured by property int h = 127; if (integerCacheHighPropValue != null) { // Use Long.decode here to avoid invoking methods that // require Integer's autoboxing cache to be initialized int i = Long.decode(integerCacheHighPropValue).intValue(); i = Math.max(i, 127); // Maximum array size is Integer.MAX_VALUE h = Math.min(i, Integer.MAX_VALUE - -low); } high = h; cache = new Integer[(high - low) + 1]; int j = low; for(int k = 0; k < cache.length; k++) cache[k] = new Integer(j++); } private IntegerCache() {} }
// value of java.lang.Integer.IntegerCache.high property (obtained during VM init) private static String integerCacheHighPropValue; static void getAndRemoveCacheProperties() { if (!sun.misc.VM.isBooted()) { Properties props = System.getProperties(); integerCacheHighPropValue = (String)props.remove("java.lang.Integer.IntegerCache.high"); if (integerCacheHighPropValue != null) System.setProperties(props); // remove from system props } }
You can adjust the maximum value of VM by setting its java.lang.Integer.IntegerCache.high. Although it is a constant in a static code block, it is assigned during initialization, so it has no impact.
Demonstrate modifying JVM parameters (eclipse):
package test1; public class Demo1 { public static void main(String[] args) { System.out.println(Integer.valueOf(200)==Integer.valueOf(200)); } }
Output results:
false
How to modify it? Look at the following?
Click the project you created, right-click - > debug as - > debug configurations - > java application - > click your own project - > arguments - > VM arguments settings - > apply - > debug
Set later results: