Integer 类的常量
MIN_VALUE : -231 , int 类型能够表示的最小值
MAX_VALUE :231 - 1 , int 类型能够表示的最大值
TYPE :表示int 类型的class实例
SIZE :32,用来以二进制补码形式表示 int 值的比特位数
BYTES :4,用来表示二进制补码形式的int值的字节数
Integer 类图
Integer
主要方法
Integer#parseInt(String, int) 方法,通过第一位字符判断正负,遍历每一位,乘以进制累加。
[图片上传失败...(image-332bc6-1607262101295)]
IntegerCache 内部类,缓存了IntegerCache.low 至 IntegerCache.high 的Integer 对象,而IntegerCache.low 为-128,IntegerCache.high 默认为127。
Integer#valueOf(int) 方法,会先查询缓存,如果命中就返回缓存对象,否则new新对象。
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
int基本类型转换成Integer包装类型时,编译器编译后,会通过此valueOf()方法转换,所以针对缓存范围内的装箱,每次返回的是同一个对象,超出缓存范围的,则每次返回不同的对象。
Integer a = 20;
Integer b = 20;
System.out.println(a == b);//true
Integer c = 200;
Integer d = 200;
System.out.println(c == d);//false








网友评论