重要的成员变量
@Native public static final int MIN_VALUE = 0x80000000;
/**
* A constant holding the maximum value an {@code int} can
* have, 2<sup>31</sup>-1.
*/
@Native public static final int MAX_VALUE = 0x7fffffff;
/**
* The {@code Class} instance representing the primitive type
* {@code int}.
*
* @since 1.1
*/
@SuppressWarnings("unchecked")
public static final Class<Integer> TYPE = (Class<Integer>) Class.getPrimitiveClass("int");
/**
* All possible chars for representing a number as a String
*/
static final char[] digits = {
'0' , '1' , '2' , '3' , '4' , '5' ,
'6' , '7' , '8' , '9' , 'a' , 'b' ,
'c' , 'd' , 'e' , 'f' , 'g' , 'h' ,
'i' , 'j' , 'k' , 'l' , 'm' , 'n' ,
'o' , 'p' , 'q' , 'r' , 's' , 't' ,
'u' , 'v' , 'w' , 'x' , 'y' , 'z'
};
@Native public static final int SIZE = 32; 32位
public static final int BYTES = SIZE / Byte.SIZE; 32/8=4 4个字节
缓存机制
private static class IntegerCache {
static final int low = -128;
static final int high; //通过设置-XX:AutoBoxCacheMax=<size>,指定high的值,默认缓存范围为[-128,127]
static final Integer cache[];
static {
// high value may be configured by property
int h = 127;
String integerCacheHighPropValue =
VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
if (integerCacheHighPropValue != null) {
try {
int i = parseInt(integerCacheHighPropValue);
i = Math.max(i, 127);
// Maximum array size is Integer.MAX_VALUE
h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
} catch( NumberFormatException nfe) {
// If the property cannot be parsed into an int, ignore it.
}
}
high = h;
cache = new Integer[(high - low) + 1];
int j = low;
for(int k = 0; k < cache.length; k++)
cache[k] = new Integer(j++);
// range [-128, 127] must be interned (JLS7 5.1.7)
assert IntegerCache.high >= 127;
}
private IntegerCache() {}
}
验证缓存代码示例:
package chapter01;
public class TestInteger {
public static void main(String [] args) {
Integer a = Integer.valueOf(6);
Integer b = new Integer(6);
Integer c = 6;
System.out.println(a==b);
System.out.println(a==c);
System.out.println(c==b);
Integer a1 = Integer.valueOf(600);
Integer b1 = new Integer(600);
Integer c1 = 600;
System.out.println("\n");
System.out.println(a1==b1);
System.out.println(a1==c1);
System.out.println(c1==b1);
}
}
打印结果:
false
true
false
false
false
false
建议:
包装类型比较是否相等使用equals()而非==