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

      <legend id='y5PVm'><style id='y5PVm'><dir id='y5PVm'><q id='y5PVm'></q></dir></style></legend>
        <bdo id='y5PVm'></bdo><ul id='y5PVm'></ul>
      <tfoot id='y5PVm'></tfoot>
    1. <small id='y5PVm'></small><noframes id='y5PVm'>

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

      Using == operator in Java to compare wrapper objects(在 Java 中使用 == 运算符比较包装器对象)

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

                <tbody id='m1Nvq'></tbody>

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

              • 本文介绍了在 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 中使用 == 运算符比较包装器对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
                How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
                Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
                Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
                How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
                How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)
                  <bdo id='CZkwf'></bdo><ul id='CZkwf'></ul>

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

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

                    • <legend id='CZkwf'><style id='CZkwf'><dir id='CZkwf'><q id='CZkwf'></q></dir></style></legend>
                    • <tfoot id='CZkwf'></tfoot>