您的位置:首页 > 编程语言 > Java开发

java中整数的相等比较

2017-09-01 21:16 211 查看
如果比较两个数值相等的Integer类型的整数,你可能会发现,用“==”比较(首先你必须明确“==”比较的是地址),有的时候返回true,而有的时候,返回false。比如:
Integer i = 128;
Integer j = 128;
System.out.println(i == j);//返回false


Integer m = 127;
Integer n = 127;
System.out.println(m == n);//返回true

为什么两个相差1的数,比较的结果却不一样呢。看一下源码

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);
}

Integer i = 128;这种方式赋值,会调用valueOf方法。我们发现这里做了一些关于IntegerCache的操作。IntegerCache源码:
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) -1);
}
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() {}
}
原来Integer把-128到127(可调)的整数都提前实例化了, 所以你不管创建多少个这个范围内的Integer都是同一个对象。

那么,如何比较两个Integer类型是否相等呢,你肯定会想到equals,没错,就是equals,看下equals源码:
public boolean equals(Object obj) {
if (obj instanceof Integer) {
return value == ((Integer)obj).intValue();
}
return false;
}

value值是int类型:

private final int value;


通过源码得知,是获取Integer的基本类型值来用==比较了。
所以,我们也可以这样,通过Integer.intValue()获取int值来直接比较。
其实,一个int类型和Integer直接“==”比较也是没有问题的:
Integer i = 128;
int j = 128;
System.out.println(i == j);//返回true

原因是Integer类型会自动拆箱变为int类型来进行比较。

综上:
如果你用两个Integer类型的整数做相等比较

1.如果Integer类型的两个数相等,如果范围在-128~127(默认),那么用“==”返回true,其余的范会false。
2.两个基本类型int进行相等比较,直接用==即可。
3.一个基本类型int和一个包装类型Integer比较,用==也可,比较时候,Integer类型做了拆箱操作。
4.Integer类型比较大小,要么调用Integer.intValue()转为基本类型用“==”比较,要么直接用equals比较。

扩展:
Long和Short类型也做了相同的处理,只不过最大值是不可调的。
参考Long的源码:

public static Long valueOf(long l) {
final int offset = 128;
if (l >= -128 && l <= 127) { // will cache
return LongCache.cache[(int)l + offset];
}
return new Long(l);
}


参考Short的源码:
public static Short valueOf(short s) {
final int offset = 128;
int sAsInt = s;
if (sAsInt >= -128 && sAsInt <= 127) { // must cache
return ShortCache.cache[sAsInt + offset];
}
return new Short(s);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: