Java中 int 和 Integer 的区别
一、区别
- int 是 Java 中的 基本数据类型 ,
Integer 是 int 的包装类 ; - int 的默认值是 0
Integer 的默认值是 null
二、 关于 Integer 和 int 的比较问题
- 第一种情况:两个 new 生成的 Integer 变量
Integer a = new Integer(1);
Integer b = new Integer(1);
System.out.println( a == b );
Integer c = new Integer(1000);
Integer d = new Integer(1000);
System.out.println( c == d );
结果
false
false
结论:因为 new 生成的是两个对象,其内存地址不同 ,
== 两边是对象时,比较的是内存地址,所以结果是 false 。
- 第二种情况:非 new 生成的 Integer 变量 和 int 变量
Integer a = 1;
int b = 1;
System.out.println( a == b );
Integer c = 1000;
int d = 1000;
System.out.println( c == d );
结果
true
true
结论:包装类 Integer 和基本数据类型 int 比较时,Integer 会自动拆包装为int,然后进行比较,实际上就变为两个 int 变量的比较 。
- 第三种情况:非 new 生成的 Integer 变量和 new 生成的 Integer 变量
Integer a = 1;
Integer b = new Integer(1);
System.out.println( a == b );
Integer c = 1000;
Integer d = new Integer(1000);
System.out.println( c == d );
结果
false
false
结论:非 new 生成的 Integer 变量指向的是java常量池中的对象,而new Integer()生成的变量指向堆中新建的对象,两者在内存中的地址不同 。
- 第四种情况:两个非 new 生成的 Integer 变量
Integer a = -128;
Integer b = -128;
System.out.println( a == b );
Integer c = -129;
Integer d = -129;
System.out.println( c == d );
Integer e = 127;
Integer f = 127;
System.out.println( e == f );
Integer g = 128;
Integer h = 128;
System.out.println( g == h );
结果
true
false
true
false
结论:java在编译 Integer a = -128; 时,
会翻译成为 Integer a = Integer.valueOf( -128 ),
java对于 -128 到 127 之间的数( 即 [-128,127] ),会进行缓存,
所以 Integer a = -128; 时,会将 -128 进行缓存,
下次再写 Integer b = -128; 时,就会直接从缓存中取,就不会new了,
后边的c、d、e、f、g、h也是同理 。
我们可以看看 Integer.java 的源码中的 valueOf(int i) 这个方法
发现传入的 i 满足条件: i >= IntegerCache.low && i <= IntegerCache.high , 就会被加入缓存,
否则,new 一个 Integer 对象 。
我们看看 IntegerCache.low 和 IntegerCache.high
/**
* 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
* sun.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 property
int h = 127;
String integerCacheHighPropValue =
sun.misc.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() {}
}
我们发现 low = -128 ,在静态代码块中有两行代码分别是 int h = 127; 和 high = h; 所以 high = 127 。
如有错误,欢迎指正;如有侵权,请联系作者。