为什么==与Integer.valueOf(String)比较给127和128不同的结果? [英] Why do == comparisons with Integer.valueOf(String) give different results for 127 and 128?

查看:352
本文介绍了为什么==与Integer.valueOf(String)比较给127和128不同的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道为什么这些代码行返回不同的值:

  System.out.println(Integer.valueOf 127)== Integer.valueOf(127)); 
System.out.println(Integer.valueOf(128)== Integer.valueOf(128));
System.out.println(Integer.parseInt(128)== Integer.valueOf(128));

输出为:

  true 
false
true

第一个返回 true ,第二个返回 false ?在 127 128 之间,我不知道有什么不同吗? (当然我知道 127 < 128 。)



此外,为什么第三个返回 true



我已阅读答案此问题,但我仍然没有得到如何可以返回 true ,以及为什么第二行中的代码返回 false

解决方案

这里有一个显着的区别。



valueOf c $ c> Integer 对象,其值可以在-128和127之间缓存。这就是为什么第一个值返回 true - 并且第二个值返回 false - 128不是一个缓存的值,所以你得到两个单独的 Integer 请务必注意您正在比较引用与 Integer#valueOf

>,如果你比较一个大于缓存支持的值,它将计算 true ,即使解析的值是等价的(例子: Integer.valueOf(128)== Integer.valueOf(128))。您必须改用 equals()



parseInt 正在返回一个原语 int 。这就是第三个值返回 true - 128 == 128 的原因,当然, true



现在,一个公平的位发生了第三个结果 true




I have no idea why these lines of code return different values:

System.out.println(Integer.valueOf("127")==Integer.valueOf("127"));
System.out.println(Integer.valueOf("128")==Integer.valueOf("128"));
System.out.println(Integer.parseInt("128")==Integer.valueOf("128"));

The output is:

true
false
true

Why does the first one return true and the second one return false? Is there something different that I don't know between 127 and 128? (Of course I know that 127 < 128.)

Also, why does the third one return true?

I have read the answer of this question, but I still didn't get how it can return true, and why the code in second line returns false.

解决方案

There's a striking difference here.

valueOf is returning an Integer object, which may have its values cached between -128 and 127. This is why the first value returns true - it's cached - and the second value returns false - 128 isn't a cached value, so you're getting two separate Integer instances.

It is important to note that you are comparing references with Integer#valueOf, and if you are comparing a value that is larger than what the cache supports, it will not evaluate to true, even if the parsed values are equivalent (case in point: Integer.valueOf(128) == Integer.valueOf(128)). You must use equals() instead.

parseInt is returning a primitive int. This is why the third value returns true - 128 == 128 is evaluated, and is of course, true.

Now, a fair bit happens to make that third result true:

  • An unboxing conversion occurs with respect to the equivalence operator you're using and the datatypes you have - namely, int and Integer. You're getting an Integer from valueOf on the right hand side, of course.

  • After the conversion, you're comparing two primitive int values. Comparison happens just as you would expect it to with respect to primitives, so you wind up comparing 128 and 128.

这篇关于为什么==与Integer.valueOf(String)比较给127和128不同的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆