<bdo id='n6lpr'></bdo><ul id='n6lpr'></ul>
    1. <tfoot id='n6lpr'></tfoot>

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

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

      1. 为什么相等运算符适用于整数值直到 128 数字?

        Why equal operator works for Integer value until 128 number?(为什么相等运算符适用于整数值直到 128 数字?)
        <i id='VGdni'><tr id='VGdni'><dt id='VGdni'><q id='VGdni'><span id='VGdni'><b id='VGdni'><form id='VGdni'><ins id='VGdni'></ins><ul id='VGdni'></ul><sub id='VGdni'></sub></form><legend id='VGdni'></legend><bdo id='VGdni'><pre id='VGdni'><center id='VGdni'></center></pre></bdo></b><th id='VGdni'></th></span></q></dt></tr></i><div id='VGdni'><tfoot id='VGdni'></tfoot><dl id='VGdni'><fieldset id='VGdni'></fieldset></dl></div>
          <bdo id='VGdni'></bdo><ul id='VGdni'></ul>

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

              <tbody id='VGdni'></tbody>

              1. <tfoot id='VGdni'></tfoot>
                  <legend id='VGdni'><style id='VGdni'><dir id='VGdni'><q id='VGdni'></q></dir></style></legend>
                  本文介绍了为什么相等运算符适用于整数值直到 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 数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

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

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

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

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

                            <tbody id='r6tNb'></tbody>