static { finalint 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++); } privateIntegerCache(){} }
其主要的功能就是把-128~127的整数缓存起来,当要使用这些数字时,就从这些缓存中获取。
由此可知下面代码运行的结果为true:
1 2 3
Integer m = 127; Integer n = 127; System.out.println(m == n);
而下面的代码则为false:
1 2 3
Integer m = 128; Integer n = 128; System.out.println(m == n);
3、可变参数:
可变参数使得可以声明一个接受一个可变数目参数的方法。
可变参数必须是方法声明中的最后一个参数。
3.1、包含可变参数的函数:
1 2 3 4 5 6 7
publicstaticintadd(int... arrays){ int sum = 0; for(Integer integer : arrays){ sum += integer; } return sum; }