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

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

        <tfoot id='1klFo'></tfoot>
        <legend id='1klFo'><style id='1klFo'><dir id='1klFo'><q id='1klFo'></q></dir></style></legend>
      1. 为什么在比较 Java 中的整数包装器时 128==128 为假

        时间:2023-10-01

          <tbody id='vn6IY'></tbody>

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

                <tfoot id='vn6IY'></tfoot>
              • <small id='vn6IY'></small><noframes id='vn6IY'>

              • <legend id='vn6IY'><style id='vn6IY'><dir id='vn6IY'><q id='vn6IY'></q></dir></style></legend>
                  <bdo id='vn6IY'></bdo><ul id='vn6IY'></ul>
                  本文介绍了为什么在比较 Java 中的整数包装器时 128==128 为假但 127==127 为真?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  class D {
                      public static void main(String args[]) {
                          Integer b2=128;
                          Integer b3=128;
                          System.out.println(b2==b3);
                      }
                  }
                  

                  输出:

                  false
                  

                  <小时>

                  class D {
                      public static void main(String args[]) {
                          Integer b2=127;
                          Integer b3=127;
                          System.out.println(b2==b3);
                      }
                  }
                  

                  输出:

                  true
                  

                  注意:-128 到 127 之间的数字为真.

                  Note: Numbers between -128 and 127 are true.

                  推荐答案

                  当您在 Java 中编译数字文字并将其分配给整数(大写 I)时,编译器会发出:

                  When you compile a number literal in Java and assign it to a Integer (capital I) the compiler emits:

                  Integer b2 =Integer.valueOf(127)
                  

                  这行代码也是在你使用自动装箱的时候生成的.

                  This line of code is also generated when you use autoboxing.

                  valueOf 的实现使得某些数字被池化",它为小于 128 的值返回相同的实例.

                  valueOf is implemented such that certain numbers are "pooled", and it returns the same instance for values smaller than 128.

                  来自 java 1.6 源代码,第 621 行:

                  From the java 1.6 source code, line 621:

                  public static Integer valueOf(int i) {
                      if(i >= -128 && i <= IntegerCache.high)
                          return IntegerCache.cache[i + 128];
                      else
                          return new Integer(i);
                  }
                  

                  high的值可以通过系统属性配置为另一个值.

                  The value of high can be configured to another value, with the system property.

                  -Djava.lang.Integer.IntegerCache.high=999

                  -Djava.lang.Integer.IntegerCache.high=999

                  如果您使用该系统属性运行程序,它将输出 true!

                  If you run your program with that system property, it will output true!

                  显而易见的结论:永远不要依赖两个引用是相同的,总是用 .equals() 方法比较它们.

                  The obvious conclusion: never rely on two references being identical, always compare them with .equals() method.

                  所以 b2.equals(b3) 将为 b2,b3 的所有逻辑相等值打印 true.

                  So b2.equals(b3) will print true for all logically equal values of b2,b3.

                  请注意,Integer 缓存不是出于性能原因,而是为了符合 JLS,第 5.1.7 节;必须为值 -128 到 127(含)提供对象标识.

                  Note that Integer cache is not there for performance reasons, but rather to conform to the JLS, section 5.1.7; object identity must be given for values -128 to 127 inclusive.

                  Integer#valueOf(int) 也记录了这种行为:

                  通过缓存频繁请求的值,此方法可能会显着提高空间和时间性能.此方法将始终缓存 -128 到 127(含)范围内的值,并可能缓存此范围之外的其他值.

                  this method is likely to yield significantly better space and time performance by caching frequently requested values. This method will always cache values in the range -128 to 127, inclusive, and may cache other values outside of this range.

                  这篇关于为什么在比较 Java 中的整数包装器时 128==128 为假但 127==127 为真?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Java 如何处理整数下溢和上溢以及如何检查它? 下一篇:Java:整数等于 vs. ==

                  相关文章

                  最新文章

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

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

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

                      <tfoot id='WSNMc'></tfoot>
                      <legend id='WSNMc'><style id='WSNMc'><dir id='WSNMc'><q id='WSNMc'></q></dir></style></legend>