久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

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

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

      <tfoot id='sYbzn'></tfoot>

      != 和 == 運算符如何處理 Java 中的整數?

      How != and == operators work on Integers in Java?(!= 和 == 運算符如何處理 Java 中的整數?)
        <tbody id='2Ig4K'></tbody>
      <tfoot id='2Ig4K'></tfoot><legend id='2Ig4K'><style id='2Ig4K'><dir id='2Ig4K'><q id='2Ig4K'></q></dir></style></legend>

            <bdo id='2Ig4K'></bdo><ul id='2Ig4K'></ul>

              <small id='2Ig4K'></small><noframes id='2Ig4K'>

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

                本文介紹了!= 和 == 運算符如何處理 Java 中的整數?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                問題描述

                下面的代碼讓我很困惑,因為它提供了兩種不同的輸出.代碼在 jdk 1.7 上測試過.

                The following code seemed really confusing to me since it provided two different outputs.The code was tested on jdk 1.7.

                public class NotEq {
                
                public static void main(String[] args) {
                
                    ver1();
                    System.out.println();
                    ver2();
                }
                
                public static void ver1() {
                    Integer a = 128;
                    Integer b = 128;
                
                    if (a == b) {
                        System.out.println("Equal Object");
                    }
                
                    if (a != b) {
                        System.out.println("Different objects");
                    }
                
                    if (a.equals(b)) {
                        System.out.println("Meaningfully equal.");
                    }
                }
                
                public static void ver2() {
                    Integer i1 = 127;
                    Integer i2 = 127;
                    if (i1 == i2) {
                        System.out.println("Equal Object");
                    }
                
                    if (i1 != i2){
                        System.out.println("Different objects");
                    }
                    if (i1.equals(i2)){
                        System.out.println("Meaningfully equal");
                    }
                }
                
                }
                

                輸出:

                [ver1 輸出]
                不同的對象
                有意義的平等.

                [ver1 output]
                Different objects
                Meaningfully equal.

                [ver2 輸出]
                相等對象
                有意義的平等

                [ver2 output]
                Equal Object
                Meaningfully equal

                為什么 == 和 != 測試會為 ver1() 和 ver2() 生成不同的結果,而相同的數字遠小于 Integer.MAX_VALUE?是否可以斷定 == 檢查大于 127 的數字(對于像代碼中顯示的 Integer 這樣的包裝類)完全是浪費時間?

                Why the == and != testing produces different results for ver1() and ver2() for same number much less than the Integer.MAX_VALUE? Can it be concluded that == checking for numbers greater than 127 (for wrapper classes like Integer as shown in the code) is totally waste of time?

                推薦答案

                為 -128 和 127 之間的值緩存整數,因此 Integer i = 127 將始終返回相同的引用.Integer j = 128 不一定會這樣做.然后,您將需要使用 equals 來測試底層 int 的相等性.

                Integers are cached for values between -128 and 127 so Integer i = 127 will always return the same reference. Integer j = 128 will not necessarily do so. You will then need to use equals to test for equality of the underlying int.

                這是 Java 語言規范:

                如果被裝箱的值 p 是真、假、一個字節或 u0000 到 u007f 范圍內的一個字符,或者一個介于 -128 和 127(包括)之間的 int 或短數字,則令 r1 和 r2 為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.

                但是對 Integer j = 128 的 2 次調用可能會返回相同的引用(不保證):

                But 2 calls to Integer j = 128 might return the same reference (not guaranteed):

                例如,內存限制較少的實現可能會緩存所有 char 和 short 值,以及 -32K 到 +32K 范圍內的 int 和 long 值.

                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.

                這篇關于!= 和 == 運算符如何處理 Java 中的整數?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                相關文檔推薦

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

                  <legend id='q43V4'><style id='q43V4'><dir id='q43V4'><q id='q43V4'></q></dir></style></legend>
                  • <bdo id='q43V4'></bdo><ul id='q43V4'></ul>
                  • <small id='q43V4'></small><noframes id='q43V4'>

                    <tfoot id='q43V4'></tfoot>

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

                          主站蜘蛛池模板: 亚洲精选一区二区 | 中文字幕一区二区三区精彩视频 | wwww.8888久久爱站网 | 99精品国自产在线观看 | 午夜男人免费视频 | 毛色毛片免费看 | 欧美成人二区 | 欧美精品在线一区 | 97精品超碰一区二区三区 | 曰韩三级| 日韩精品一区二区三区在线观看 | 国产色| 国产在线观看网站 | 亚洲精品一区二区三区在线 | 亚洲一区二区三区免费在线观看 | 91高清视频在线 | 国产在线二区 | 国产一区精品在线 | 国产美女精品 | 99精品视频在线观看免费播放 | 中文字幕在线观看视频一区 | 久草成人网 | 中国黄色毛片视频 | 日本成人一区二区 | 久久久久国产精品一区二区 | 色999视频 | 九九热精品视频 | 黄色免费观看网站 | 欧美中文字幕在线 | 国产精品久久久久无码av | 亚洲精品视频一区 | 国产精品精品久久久久久 | 久精品视频 | 久久av一区二区三区 | 欧美不卡在线 | 国产成人网 | 亚洲欧美在线视频 | 四虎最新地址 | 欧美一区二区三区大片 | 成人自拍视频网站 | 亚洲xxxxx|