Integer 和 Integer 的比较
对于Integer和Integer的 == (比较的是地址),我们注重几点:
- 在Integer中有一个静态的内部类IntegerCache,里面有一个cache[], 也就是Integer常量池,常量池缓存的大小为[-128 ~ 127]
- 当我们赋值的数字在这个范围内时,不需要创建新的对象,而是从缓存中获取已经创建好的Integer对象
- 当超出这个范围时,直接new Integer来创建Integer对象
/*** Cache to support the object identity semantics of autoboxing for values between* -128 and 127 (inclusive) as required by JLS.** The cache is initialized on first usage. The size of the cache* may be controlled by the {@code -XX:AutoBoxCacheMax=<size>} option.* During VM initialization, java.lang.Integer.IntegerCache.high property* may be set and saved in the private system properties in the* jdk.internal.misc.VM class.*/private static class IntegerCache {static final int low = -128;static final int high;static final Integer cache[];static {// high value may be configured by propertyint 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_VALUEh = 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() {}}
如果比较使用的是equals(),则比较的是数值
public boolean equals(Object obj) {if (obj instanceof Integer) {return value == ((Integer)obj).intValue();}return false;}
Integer 和 int 的比较
对于Integer和int的 == (比较的是数值),我们注重几点:
对于创建Integer来说,有两种方法:
一种是Integer in1 = 10; 等同于 Integer in1 = Integer.valueOf (10) 自动装箱
另一种是Integer in4 = new Integer(10); 装箱
不论哪一种方法,只要与int类型进行比较,那么 == 就会使Interger拆包成int类型,就是相等的。
关于拆箱:
int b1 = in1; 自动拆箱
int b2 = in1.intValue(); 拆箱
关于自动装箱:static进行加载,将int转化为Integer进行装箱
public static Integer valueOf(int i) {assert IntegerCache.high >= 127;if (i >= IntegerCache.low && i <= IntegerCache.high)return IntegerCache.cache[i + (-IntegerCache.low)];return new Integer(i);}
关于自动拆箱:
public static void main(String[] args) {// TODO Auto-generated method stubInteger b=new Integer(3); //b为Integer的对象int a=b; //a为一个int的基本数据类型System.out.println(a); //打印输出3。}
int a=b,很奇怪,怎么把一个对象赋给了一个基本类型呢?其实 int a = b,这段代码等价于:int a=b.intValue(),来看看 inValue() 方法到底是什么,下面是源码:
public int intValue() {return value;}
这个方法很简单嘛,就是返回了value值,然而value又是什么,继续找到了一个代码:
public Integer(int value) {this.value = value;}
原来value,就是定义 Integer b=new Integer(3) ; 赋的值。所以上面的代码其实是这样的:
public static void main(String[] args) {// TODO Auto-generated method stubInteger b=new Integer(3); //b为Integer的对象int a=b.intValue(); //其中b.intValue()返回实例化b时构造函数new Integer(3);赋的值3。System.out.println(a); //打印输出3。}
int 和 int 的比较
直接比较数值即可
、
int i = 10;int i0 = 10; // 栈上Integer in1 = 10; // 由于直接赋值的时候会进行自动的装箱(装包)Integer in2 = 10; Integer in3 = new Integer(10);// 堆上且开辟了一个新的空间地址Integer in4 = new Integer(10);Integer in5 = 199;Integer in6 = 199;// Integer 与 Integer 比较的是地址// int 和 int 比较的是数值System.out.println(i == i0); // true 数值比较System.out.println(i == in1); // true 数值比较 拆箱变成int类型System.out.println(i == in3); // true 数值比较 拆箱变成int类型System.out.println(in1 == in2); // true 地址相同System.out.println(in1 == in3); // false 地址不同System.out.println(in3 == in4); // false 地址不同System.out.println(in5 == in6); // false 超过[-128,127],重新new一个Integer对象