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

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

    1. <tfoot id='KJvTL'></tfoot>

        在 Java 中使用 == 运算符比较包装器对象

        时间:2023-10-01

              <tbody id='N5hbG'></tbody>

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

              <tfoot id='N5hbG'></tfoot>
                <bdo id='N5hbG'></bdo><ul id='N5hbG'></ul>
              • <legend id='N5hbG'><style id='N5hbG'><dir id='N5hbG'><q id='N5hbG'></q></dir></style></legend>
                <i id='N5hbG'><tr id='N5hbG'><dt id='N5hbG'><q id='N5hbG'><span id='N5hbG'><b id='N5hbG'><form id='N5hbG'><ins id='N5hbG'></ins><ul id='N5hbG'></ul><sub id='N5hbG'></sub></form><legend id='N5hbG'></legend><bdo id='N5hbG'><pre id='N5hbG'><center id='N5hbG'></center></pre></bdo></b><th id='N5hbG'></th></span></q></dt></tr></i><div id='N5hbG'><tfoot id='N5hbG'></tfoot><dl id='N5hbG'><fieldset id='N5hbG'></fieldset></dl></div>
                  本文介绍了在 Java 中使用 == 运算符比较包装器对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在阅读 Kathy Sierra 和 Bert Bates 的 SCJP Java 6,这本书让我非常困惑.在第 245 页上,他们声明以下代码.

                  I'm reading SCJP Java 6 by Kathy Sierra and Bert Bates and this book is confusing me so much. On page 245 they state that the following code below.

                  Integer i1 = 1000;
                  Integer i2 = 1000;
                  if(i1 != i2)
                  System.out.println("different objects");
                  
                  //Prints output
                  different objects
                  

                  然后在下一页他们有以下代码

                  Then on the very next page they have the following code

                  Integer i3 = 10;
                  Integer i4 = 10;
                  if(i3 == i4)
                  System.out.println("same objects");
                  
                  //Prints output
                  same objects
                  

                  我很困惑!当我自己尝试时,您似乎无法使用 == 来比较使用 equals() 方法的相同方式.即使整数变量设置为相同的值(即 10),使用 == 总是会给我假".我对么?使用 == 比较相同的 Integer 对象(具有相同的值)将始终导致 'false'

                  I'm so confused! When I try this out on my own it seems that you cannot use the == to compare the same way you would use equals() method. Using the == always gives me 'false' even if the Integer variables are set to the same value (i.e. 10). Am I correct? Using the == to compare the same Integer object (with same values) will always result in 'false'

                  推荐答案

                  答案的关键就叫object interning.Java 实习生的数字很小(小于 128),因此所有 Integer(n) 的实例与 n 在实习范围内都是相同的.大于或等于 128 的数字不会被保留,因此 Integer(1000) 对象彼此不相等.

                  The key to the answer is called object interning. Java interns small numbers (less than 128), so all instances of Integer(n) with n in the interned range are the same. Numbers greater than or equal to 128 are not interned, hence Integer(1000) objects are not equal to each other.

                  这篇关于在 Java 中使用 == 运算符比较包装器对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:用逗号将整数转换为字符串数千 下一篇:为什么相等运算符适用于整数值直到 128 数字?

                  相关文章

                  最新文章

                  • <bdo id='FLybb'></bdo><ul id='FLybb'></ul>

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