请判断如下题目的输出结果
public class IntegerTest {
public static void main(String[] args) {
Integer a=100, b = 100, c=500, d=500;
System.out.println(a == b);
System.out.println(c == d);
}
}
你可能会认为要么全为true,要么全为false;也可能你确实对这些东西了解过了,认为第一个为true,第二个为false。但是你清楚为什么吗?其实全不对,这个题目的结果是不确定的。
1.首先我们先来了解一下Integer与int的关系
1.1java中四类八种基本数据类型,以及封装类型
数值型:byteshort int long
字符型:char
浮点型:floatdouble
布尔型:boolean
基本数据类型 | byte(字节型) | short(短整型)) | int(整型) | long(长整型) | float(浮点型) | double(双精度) | char(字符型) | boolean(布尔型) |
占用字节 | 1 | 2 | 4 | 8 | 4 | 8 | 2 | 系统没有提供size方法 |
默认值 | 0 | 0 | 0 | 0.0l | 0.0f | 0.0 | /u0000(空格) | false |
封装器类 | Byte | Short | Integer | Long | Float | Double | Character | Boolean |
1.2 区分基本类型与封装类型的好处
网上有很多说明,这里就不再赘述了
2.数值比较为什么不确定?
2.1 赋值f1 = 100时,java内部发生了什么
/**
* Returns an {@code Integer} instance representing the specified
* {@code int} value. If a new {@code Integer} instance is not
* required, this method should generally be used in preference to
* the constructor {@link #Integer(int)}, as this method is likely
* to yield significantly better space and time performance by
* caching frequently requested values.
*
* This method will always cache values in the range -128 to 127,
* inclusive, and may cache other values outside of this range.
*
* @param i an {@code int} value.
* @return an {@code Integer} instance representing {@code i}.
* @since 1.5
*/
public static Integer valueOf(int i) {
assert IntegerCache.high >= 127;// vm配置最大值不能小于127
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
assert IntegerCache.high >= 127;// vm配置最大值不能小于127
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
/**
* 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 -XX:AutoBoxCacheMax=<size> option. 通过该参数调整最大值high
* 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) {
int i = parseInt(integerCacheHighPropValue);
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++);
}
private IntegerCache() {}
}
-XX:AutoBoxCacheMax=<size> option. 通过该参数调整最大值high
* 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) {
int i = parseInt(integerCacheHighPropValue);
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++);
}
private IntegerCache() {}
}
从上面可以了解到,树智的比较与IntegerCache的low与high相关,而low为默认值-128,high默认为127;所以有一个说法当integer 取值范围-128至127之间时由于是通过 cache[]中获取所以比较值应该为true;
但是文章内部也会看到 最大值其实是可以变化的,通过如下信息获得,也就是与vm配置相关,但同时这个配置值不能小于127
<span style="color:#ff0000">-XX:AutoBoxCacheMax=<size></span>
java.lang.Integer.IntegerCache.high
3.总结
Integer -128至127之间的数值比较结果:true
小于-128之间的树智比较结果:false
大于127之间的树智比较结果:不确定,与虚拟机配置参数有关
4.趣味思考
思考一个问题: Integer a = 200; int b = 200; a==b?
结果:相等,考虑下为什么?