• <bdo id='1bUTN'></bdo><ul id='1bUTN'></ul>

    <small id='1bUTN'></small><noframes id='1bUTN'>

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

      1. <i id='1bUTN'><tr id='1bUTN'><dt id='1bUTN'><q id='1bUTN'><span id='1bUTN'><b id='1bUTN'><form id='1bUTN'><ins id='1bUTN'></ins><ul id='1bUTN'></ul><sub id='1bUTN'></sub></form><legend id='1bUTN'></legend><bdo id='1bUTN'><pre id='1bUTN'><center id='1bUTN'></center></pre></bdo></b><th id='1bUTN'></th></span></q></dt></tr></i><div id='1bUTN'><tfoot id='1bUTN'></tfoot><dl id='1bUTN'><fieldset id='1bUTN'></fieldset></dl></div>
      2. 为什么相等运算符适用于整数值直到 128 数字?

        时间:2023-10-01
          <bdo id='bV136'></bdo><ul id='bV136'></ul>

            • <legend id='bV136'><style id='bV136'><dir id='bV136'><q id='bV136'></q></dir></style></legend>

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

                <tbody id='bV136'></tbody>
              <tfoot id='bV136'></tfoot>
                1. <i id='bV136'><tr id='bV136'><dt id='bV136'><q id='bV136'><span id='bV136'><b id='bV136'><form id='bV136'><ins id='bV136'></ins><ul id='bV136'></ul><sub id='bV136'></sub></form><legend id='bV136'></legend><bdo id='bV136'><pre id='bV136'><center id='bV136'></center></pre></bdo></b><th id='bV136'></th></span></q></dt></tr></i><div id='bV136'><tfoot id='bV136'></tfoot><dl id='bV136'><fieldset id='bV136'></fieldset></dl></div>
                  本文介绍了为什么相等运算符适用于整数值直到 128 数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  为什么整数 == 运算符不适用于 128 及之后的整数值?有人能解释一下这种情况吗?

                  Why Integer == operator does not work for 128 and after Integer values? Can someone explain this situation?

                  这是我的 Java 环境:

                  This is my Java environment:

                  java version "1.6.0_37"
                  Java(TM) SE Runtime Environment (build 1.6.0_37-b06)
                  Java HotSpot(TM) 64-Bit Server VM (build 20.12-b01, mixed mode)
                  

                  示例代码:

                  Integer a;
                  Integer b;
                  a = 129;
                  b = 129;
                  
                  for (int i = 0; i < 200; i++) {
                      a = i;
                      b = i;
                  
                      if (a != b) {
                          System.out.println("Value:" + i + " - Different values");
                      } else {
                          System.out.println("Value:" + i + " - Same values");
                      }
                  }
                  

                  部分控制台输出:

                  Value:124 - Same values
                  Value:125 - Same values
                  Value:126 - Same values
                  Value:127 - Same values
                  Value:128 - Different values
                  Value:129 - Different values
                  Value:130 - Different values
                  Value:131 - Different values
                  Value:132 - Different values
                  

                  推荐答案

                  查看Integer 的源代码 .您可以在那里看到值的缓存.

                  Check out the source code of Integer . You can see the caching of values there.

                  缓存仅在您使用 Integer.valueOf(int) 时发生,而不是在您使用 new Integer(int) 时发生.您使用的自动装箱使用 Integer.valueOf.

                  The caching happens only if you use Integer.valueOf(int), not if you use new Integer(int). The autoboxing used by you uses Integer.valueOf.

                  根据 JLS,您始终可以相信,对于介于 -128 和 127 之间的值,在自动装箱后您会获得相同的 Integer 对象,并且在某些实现中,即使对于更高的值,您也可能会获得相同的对象.

                  According to the JLS, you can always count on the fact that for values between -128 and 127, you get the identical Integer objects after autoboxing, and on some implementations you might get identical objects even for higher values.

                  实际上在 Java 7 中(我认为在 Java 6 的较新版本中),IntegerCache 类的实现 已更改,上限不再是硬编码,但可以通过属性java.lang.Integer.IntegerCache.high"进行配置,所以如果你使用 VM 参数 -Djava.lang.Integer.IntegerCache.high=1000 运行您的程序,您将获得相同的值";所有值.

                  Actually in Java 7 (and I think in newer versions of Java 6), the implementation of the IntegerCache class has changed, and the upper bound is no longer hardcoded, but it is configurable via the property "java.lang.Integer.IntegerCache.high", so if you run your program with the VM parameter -Djava.lang.Integer.IntegerCache.high=1000, you get "Same values" for all values.

                  但是 JLS 仍然保证它只到 127:

                  But the JLS still guarantees it only until 127:

                  理想情况下,装箱一个给定的原始值 p,总是会产生一个相同的引用.在实践中,使用现有的实现技术这可能是不可行的.上述规则是一种务实的妥协.上面的最后一个条款要求某些常见的值总是被装箱到无法区分的对象中.实现可能会延迟或急切地缓存这些内容.

                  Ideally, boxing a given primitive value p, would always yield an identical reference. In practice, this may not be feasible using existing implementation techniques. The rules above are a pragmatic compromise. The final clause above requires that certain common values always be boxed into indistinguishable objects. The implementation may cache these, lazily or eagerly.

                  对于其他值,此公式不允许程序员对装箱值的身份进行任何假设.这将允许(但不要求)共享部分或全部这些引用.

                  For other values, this formulation disallows any assumptions about the identity of the boxed values on the programmer's part. This would allow (but not require) sharing of some or all of these references.

                  这可确保在最常见的情况下,行为将是所需的行为,而不会造成过度的性能损失,尤其是在小型设备上.例如,内存限制较少的实现可能会缓存所有字符和短整数,以及 -32K - +32K 范围内的整数和长整数.

                  This ensures that in most common cases, the behavior will be the desired one, without imposing an undue performance penalty, especially on small devices. Less memory-limited implementations might, for example, cache all characters and shorts, as well as integers and longs in the range of -32K - +32K.

                  这篇关于为什么相等运算符适用于整数值直到 128 数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:在 Java 中使用 == 运算符比较包装器对象 下一篇:在java中,使用byte或short而不是int和float而不是d

                  相关文章

                  最新文章

                    <bdo id='znZlW'></bdo><ul id='znZlW'></ul>

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

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