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

      <bdo id='J4lj2'></bdo><ul id='J4lj2'></ul>
  • <small id='J4lj2'></small><noframes id='J4lj2'>

    <tfoot id='J4lj2'></tfoot>

      1. 整数包装对象仅在值 127 内共享相同的实例?

        时间:2023-05-29
        • <bdo id='5q0o0'></bdo><ul id='5q0o0'></ul>

          • <tfoot id='5q0o0'></tfoot>
              <tbody id='5q0o0'></tbody>
              <legend id='5q0o0'><style id='5q0o0'><dir id='5q0o0'><q id='5q0o0'></q></dir></style></legend>

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

                • 本文介绍了整数包装对象仅在值 127 内共享相同的实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  这里它们是同一个实例:

                  Here they are the same instance:

                  Integer integer1 = 127;
                  Integer integer2 = 127;
                  System.out.println(integer1 == integer2);  // outputs "true"
                  

                  但这里它们是不同的实例:

                  But here they are different instances:

                  Integer integer1 = 128;
                  Integer integer2 = 128;
                  System.out.println(integer1 == integer2);  // outputs "false"
                  

                  为什么包装对象只在值 127 内共享同一个实例?

                  Why do the wrapper objects share the same instance only within the value 127?

                  推荐答案

                  因为它是由Java语言规范指定的.

                  Because it's specified by Java Language Specification.

                  JLS 5.1.7拳击转换:

                  如果被装箱的值 ptruefalsebyteu0000u007f 范围内的字符,或介于 - 之间的 intshort 数字128和127(含),则令r1r2为任意两次装箱转换的结果p.r1 == r2 总是如此.

                  If the value p being boxed is true, false, a byte, or a char in the range u0000 to u007f, or an int or short number between -128 and 127 (inclusive), then let r1 and r2 be the results of any two boxing conversions of p. It is always the case that r1 == r2.

                  理想情况下,对给定的原始值 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.

                  这可确保在大多数常见情况下,行为将是所需的行为,而不会造成过度的性能损失,尤其是在小型设备上.例如,内存限制较少的实现可能缓存所有 charshort 值,以及 intlong 值在 -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 char and short values, as well as int and long values in the range of -32K to +32K.

                  这篇关于整数包装对象仅在值 127 内共享相同的实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何防止 java.lang.NumberFormatException:对于输入字符串:“N/A"? 下一篇:Java中的整数和int有什么区别?

                  相关文章

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

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