请看如下代码:
1、大家猜猜看打印结果是什么?
public class Test {
public static void main(String[] args) {
Short a = 1;Short b = 1;
System.out.println(a == b);
System.out.println(a.equals(b));
}
}
2、如下代码大家猜猜看结果
public class Test {
public static void main(String[] args) {
Short e = 128;Short f = 128;
System.out.println(e == f);
System.out.println(e.equals(f));
}
}
3、如下代码大家猜猜看结果public class Test {
public static void main(String[] args) {
Short a = 1;Short b = 1;
Short c = 127;
Short d = 127;
Short e = 128;
Short f = 128;
Short g = 129;
Short h = 129;
System.out.println(a == b);
System.out.println(a.equals(b));
System.out.println(c == d);
System.out.println(c.equals(d));
System.out.println(e == f);
System.out.println(e.equals(f));
System.out.println(g == h);
System.out.println(g.equals(h));
}
}
以上代码结果如下
第一个代码:
true
true
第二个代码:
false
true
第三个代码:
true
true
true
true
false
true
false
true
很奇怪吧,当Short类型的数据,如果小于等于127的时候使用==和使用equal方法会得到相同的结果。I
当大于等于128的时候,使用==和使用equal方法得到的结果会相反。
所以比较Short类型的数据要使用equal方法才是正确的。
默认情况下Integer也是相同的结果。
所以不管Double Float Long Integer Short....都使用equal方法比较。
应该是java自动装箱时候的池问题。类似于String池。后来看了一下代码果然是这样。
下面是Short包装类的缓存池。
private static class ShortCache {
private ShortCache(){}
static final Short cache[] = new Short[-(-128) + 127 + 1];
static {
for(int i = 0; i < cache.length; i++)
cache[i] = new Short((short)(i - 128));
}
}
刚才为什么说默认情况下Integer也是相同的结果呢?
下面 请看Intege类的代码:
private static class IntegerCache {
static final int high;
static final Integer cache[];
static {
final int low = -128;
// high value may be configured by property
int h = 127;
if (integerCacheHighPropValue != null) {
// Use Long.decode here to avoid invoking methods that
// require Integer's autoboxing cache to be initialized
int i = Long.decode(integerCacheHighPropValue).intValue();
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() {}
}
/**
* 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. During VM initialization the
* getAndRemoveCacheProperties method may be used to get and remove any system
* properites that configure the cache size. At this time, the size of the
* cache may be controlled by the vm option -XX:AutoBoxCacheMax=<size>.
*/
// value of java.lang.Integer.IntegerCache.high property (obtained during VM init)
private static String integerCacheHighPropValue;
static void getAndRemoveCacheProperties() {
if (!sun.misc.VM.isBooted()) {
Properties props = System.getProperties();
integerCacheHighPropValue =
(String)props.remove("java.lang.Integer.IntegerCache.high");
if (integerCacheHighPropValue != null)
System.setProperties(props); // remove from system props
}
}
可以看出Integer的自动装箱池是可配置的,怎么配置?根据注释中的提示,设置运行时候的vm arguments -xx:AutoBoxCacheMax=256
把 256换成你想要的值即可。