Java中Integer类型的整数值的大小比较

时间:2025-01-24 21:35:14

如果比较两个数值相等的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

为什么会出现这种请况呢,因为Integer i = 128;这种方式赋值,会调用valueOf方法。我们发现这里做了一些关于IntegerCache的操作。让我们先看下valueOf的源码:

这里补充一下assert(断言)的用法:(

assert格式

(1)assert [boolean 表达式]

如果[boolean表达式]为true,则程序继续执行。

如果为false,则程序抛出AssertionError,并终止执行。

(2)assert[boolean 表达式 : 错误表达式 (日志)]

如果[boolean表达式]为true,则程序继续执行。

如果为false,则程序抛出java.lang.AssertionError,输出[错误信息]。

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

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;
//把-128到127(可调)的整数都提前实例化了
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类型的整数做相等比较:

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

相关文章