<bdo id='gvGs7'></bdo><ul id='gvGs7'></ul>
  • <tfoot id='gvGs7'></tfoot>
    <legend id='gvGs7'><style id='gvGs7'><dir id='gvGs7'><q id='gvGs7'></q></dir></style></legend>

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

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

        Java:整数等于 vs. ==

        Java: Integer equals vs. ==(Java:整数等于 vs. ==)
      2. <small id='TqFad'></small><noframes id='TqFad'>

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

          <legend id='TqFad'><style id='TqFad'><dir id='TqFad'><q id='TqFad'></q></dir></style></legend>

              <tfoot id='TqFad'></tfoot>
              • <bdo id='TqFad'></bdo><ul id='TqFad'></ul>

                    <tbody id='TqFad'></tbody>

                  本文介绍了Java:整数等于 vs. ==的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  从 Java 1.5 开始,您几乎可以在许多情况下将 Integerint 互换.

                  As of Java 1.5, you can pretty much interchange Integer with int in many situations.

                  但是,我发现我的代码中有一个潜在的缺陷,这让我有点吃惊.

                  However, I found a potential defect in my code that surprised me a bit.

                  以下代码:

                  Integer cdiCt = ...;
                  Integer cdsCt = ...;
                  ...
                  if (cdiCt != null && cdsCt != null && cdiCt != cdsCt)
                      mismatch = true;
                  

                  当值相等时,似乎错误地设置了不匹配,尽管我无法确定在什么情况下.我在 Eclipse 中设置了一个断点,发现 Integer 的值都是 137,我检查了布尔表达式,它说它是假的,但是当我越过它时,它将不匹配设置为真.

                  appeared to be incorrectly setting mismatch when the values were equal, although I can't determine under what circumstances. I set a breakpoint in Eclipse and saw that the Integer values were both 137, and I inspected the boolean expression and it said it was false, but when I stepped over it, it was setting mismatch to true.

                  将条件更改为:

                  if (cdiCt != null && cdsCt != null && !cdiCt.equals(cdsCt))
                  

                  解决了问题.

                  谁能解释一下为什么会这样?到目前为止,我只在我自己的 PC 上的本地主机上看到了这种行为.在这种特殊情况下,代码成功通过了大约 20 次比较,但在 2 次比较失败.问题始终可以重现.

                  Can anyone shed some light on why this happened? So far, I have only seen the behavior on my localhost on my own PC. In this particular case, the code successfully made it past about 20 comparisons, but failed on 2. The problem was consistently reproducible.

                  如果这是一个普遍存在的问题,它应该会导致我们的其他环境(开发和测试)出现错误,但到目前为止,在执行此代码片段的数百次测试之后,没有人报告此问题.

                  If it is a prevalent problem, it should be causing errors on our other environments (dev and test), but so far, no one has reported the problem after hundreds of tests executing this code snippet.

                  使用 == 比较两个 Integer 值仍然不合法吗?

                  Is it still not legitimate to use == to compare two Integer values?

                  除了下面所有的好答案之外,下面的 stackoverflow 链接还有很多额外的信息.它实际上会回答我原来的问题,但是因为我没有在我的问题中提到自动装箱,所以它没有出现在选定的建议中:

                  In addition to all the fine answers below, the following stackoverflow link has quite a bit of additional information. It actually would have answered my original question, but because I didn't mention autoboxing in my question, it didn't show up in the selected suggestions:

                  为什么编译器/JVM就不能让自动装箱正常工作"?

                  推荐答案

                  JVM 正在缓存整数值.因此,与 == 的比较仅适用于 -128 到 127 之间的数字.

                  The JVM is caching Integer values. Hence the comparison with == only works for numbers between -128 and 127.

                  参考:#Immutable_Objects_.2F_Wrapper_Class_Caching

                  这篇关于Java:整数等于 vs. ==的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How can I detect integer overflow on 32 bits int?(如何检测 32 位 int 上的整数溢出?)
                  Local variables before return statements, does it matter?(return 语句之前的局部变量,这有关系吗?)
                  How to convert Integer to int?(如何将整数转换为整数?)
                  How do I create an int array with randomly shuffled numbers in a given range(如何在给定范围内创建一个随机打乱数字的 int 数组)
                  Inconsistent behavior on java#39;s ==(java的行为不一致==)
                  Why is Java able to store 0xff000000 as an int?(为什么 Java 能够将 0xff000000 存储为 int?)

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

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