java integer 值比较大小_Java中Integer类型的整数值的大小比较

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

}

复制代码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值