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

 如果比较两个数值相等的Integer类型的整数,我们可能会发现,用“==”比较(首先你必须明确“==”比较的是地址),有的时候返回true,而有的时候,返回false。比如:

 
  1. Integer i = 128;

  2. Integer j = 128;

  3. System.out.println(i == j);//返回false

然而:

 
  1. Integer m = 127;

  2. Integer n = 127;

  3. 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,输出[错误信息]。

 
  1.  
  2. public static Integer valueOf(int i) {

  3. assert IntegerCache.high >= 127;

  4. if (i >= IntegerCache.low && i <= IntegerCache.high)

  5. return IntegerCache.cache[i + (-IntegerCache.low)];

  6. return new Integer(i);

  7. }

IntegerCache源码:

 
  1. private static class IntegerCache {

  2. static final int low = -128;

  3. static final int high;

  4. static final Integer cache[];

  5.  
  6. static {

  7. // high value may be configured by property 即是可调大小的

  8. int h = 127;

  9. String integerCacheHighPropValue =

  10. sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");

  11. if (integerCacheHighPropValue != null) {

  12. int i = parseInt(integerCacheHighPropValue);

  13. i = Math.max(i, 127);

  14. // Maximum array size is Integer.MAX_VALUE

  15. h = Math.min(i, Integer.MAX_VALUE - (-low) -1);

  16. }

  17. high = h;

  18.  
  19. //把-128到127(可调)的整数都提前实例化了

  20. cache = new Integer[(high - low) + 1];

  21. int j = low;

  22. for(int k = 0; k < cache.length; k++)

  23. cache[k] = new Integer(j++);

  24. }

  25.  
  26. private IntegerCache() {}

  27. }

      原来Integer把-128到127(可调)的整数都提前实例化了, 所以你不管创建多少个这个范围内的Integer都是同一个对象。

那么,如何比较两个Integer类型是否相等呢,你肯定会想到equals,没错,就是equals,看下equals源码:

 
  1. public boolean equals(Object obj) {

  2. if (obj instanceof Integer) {

  3. return value == ((Integer)obj).intValue();

  4. }

  5. return false;

  6. }

value值是int类型(查看源码可知):

private final int value;

 通过源码得知,是获取Integer的基本类型值来用==比较了。所以,我们也可以这样,通过Integer.intValue()获取int值来直接比较。

其实,一个int类型和Integer直接“==”比较也是没有问题的,下面给出演示:

 
  1. Integer i = 128;

  2. int j = 128;

  3. 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的源码: 

 
  1.  
  2. public static Long valueOf(long l) {

  3. final int offset = 128;

  4. if (l >= -128 && l <= 127) { // will cache

  5. return LongCache.cache[(int)l + offset];

  6. }

  7. return new Long(l);

  8. }

参考Short的源码:

 
  1.  
  2. public static Short valueOf(short s) {

  3. final int offset = 128;

  4. int sAsInt = s;

  5. if (sAsInt >= -128 && sAsInt <= 127) { // must cache

  6. return ShortCache.cache[sAsInt + offset];

  7. }

  8. return new Short(s);

  9. }

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值