久久久久久久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>

                          主站蜘蛛池模板: 乳色吐息在线观看 | 久久伊人精品 | 欧美日一区| 怡红院怡春院一级毛片 | 国产精品明星裸体写真集 | 在线成人免费观看 | 午夜丰满寂寞少妇精品 | 久久精品亚洲精品国产欧美 | 九九热在线观看视频 | 欧美日韩最新 | 嫩草视频在线免费观看 | 在线观看国产wwwa级羞羞视频 | 99精品视频在线观看 | 在线播放中文字幕 | 成人精品国产一区二区4080 | 欧美一级三级在线观看 | 国产成人在线一区 | 欧美乱大交xxxxx另类电影 | 天天操夜夜拍 | 欧美一级在线观看 | 在线观看欧美日韩视频 | 国产一区二区精品在线 | 最近日韩中文字幕 | 久久久亚洲精品视频 | 一级黄色影片在线观看 | 中文在线视频 | 久久高清| 一级特黄色毛片 | 九九av| 毛片一级片 | 精品亚洲国产成av人片传媒 | 国产一区不卡 | 亚洲精品国产成人 | 波波电影院一区二区三区 | 小川阿佐美pgd-606在线 | 精品一区二区在线观看 | 在线免费小视频 | 国产在线观看av | 看a级黄色毛片 | 亚洲人精品午夜 | 色综合色综合色综合 |