<bdo id='5vlxg'></bdo><ul id='5vlxg'></ul>

  1. <legend id='5vlxg'><style id='5vlxg'><dir id='5vlxg'><q id='5vlxg'></q></dir></style></legend>

  2. <i id='5vlxg'><tr id='5vlxg'><dt id='5vlxg'><q id='5vlxg'><span id='5vlxg'><b id='5vlxg'><form id='5vlxg'><ins id='5vlxg'></ins><ul id='5vlxg'></ul><sub id='5vlxg'></sub></form><legend id='5vlxg'></legend><bdo id='5vlxg'><pre id='5vlxg'><center id='5vlxg'></center></pre></bdo></b><th id='5vlxg'></th></span></q></dt></tr></i><div id='5vlxg'><tfoot id='5vlxg'></tfoot><dl id='5vlxg'><fieldset id='5vlxg'></fieldset></dl></div>
  3. <tfoot id='5vlxg'></tfoot>

      <small id='5vlxg'></small><noframes id='5vlxg'>

    1. 为什么 == 与 Integer.valueOf(String) 的比较对 127 和

      时间:2023-10-01
        1. <small id='LeOV6'></small><noframes id='LeOV6'>

        2. <i id='LeOV6'><tr id='LeOV6'><dt id='LeOV6'><q id='LeOV6'><span id='LeOV6'><b id='LeOV6'><form id='LeOV6'><ins id='LeOV6'></ins><ul id='LeOV6'></ul><sub id='LeOV6'></sub></form><legend id='LeOV6'></legend><bdo id='LeOV6'><pre id='LeOV6'><center id='LeOV6'></center></pre></bdo></b><th id='LeOV6'></th></span></q></dt></tr></i><div id='LeOV6'><tfoot id='LeOV6'></tfoot><dl id='LeOV6'><fieldset id='LeOV6'></fieldset></dl></div>
          • <bdo id='LeOV6'></bdo><ul id='LeOV6'></ul>
            <legend id='LeOV6'><style id='LeOV6'><dir id='LeOV6'><q id='LeOV6'></q></dir></style></legend>

                <tbody id='LeOV6'></tbody>

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

                问题描述

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

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

                输出是:

                true
                false
                true
                

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

                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.)

                还有,为什么第三个返回true?

                Also, why does the third one return true?

                我已阅读这个问题的答案,但我还是没搞懂怎么返回true,为什么第二行的代码返回false.

                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 正在返回一个 Integer 对象,该对象的值可能缓存在 -128 和 127 之间.这就是第一个值返回 true - 它已缓存 - 第二个值返回 false - 128 不是缓存值,因此您将获得两个单独的 Integer 实例.

                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.

                请务必注意,您将引用与 Integer#valueOf 进行比较,如果您比较的值大于缓存支持的值,它将not 评估为 true,即使解析的值是等价的(例如:Integer.valueOf(128) == Integer.valueOf(128)).您必须改用 equals().

                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 正在返回一个原始 int.这就是为什么第三个值返回 true - 128 == 128 被评估,当然是 true.

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

                现在,恰好使第三个结果 true:

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

                • 一个拆箱转换发生在 您正在使用的等价运算符和您拥有的数据类型 - 即 intInteger.当然,您会从右侧的 valueOf 获得一个 Integer.

                • 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.

                转换后,您将比较两个原始 int 值.比较会按照您对原语的预期进行,因此您最终会比较 128128.

                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 给出不同的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                    1. <legend id='sX3oG'><style id='sX3oG'><dir id='sX3oG'><q id='sX3oG'></q></dir></style></legend>

                        <tbody id='sX3oG'></tbody>
                        <bdo id='sX3oG'></bdo><ul id='sX3oG'></ul>
                        <i id='sX3oG'><tr id='sX3oG'><dt id='sX3oG'><q id='sX3oG'><span id='sX3oG'><b id='sX3oG'><form id='sX3oG'><ins id='sX3oG'></ins><ul id='sX3oG'></ul><sub id='sX3oG'></sub></form><legend id='sX3oG'></legend><bdo id='sX3oG'><pre id='sX3oG'><center id='sX3oG'></center></pre></bdo></b><th id='sX3oG'></th></span></q></dt></tr></i><div id='sX3oG'><tfoot id='sX3oG'></tfoot><dl id='sX3oG'><fieldset id='sX3oG'></fieldset></dl></div>
                        <tfoot id='sX3oG'></tfoot>

                        <small id='sX3oG'></small><noframes id='sX3oG'>