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

      != 和 == 運(yùn)算符如何處理 Java 中的整數(shù)?

      How != and == operators work on Integers in Java?(!= 和 == 運(yùn)算符如何處理 Java 中的整數(shù)?)
        <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>

                本文介紹了!= 和 == 運(yùn)算符如何處理 Java 中的整數(shù)?的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

                問(wèn)題描述

                下面的代碼讓我很困惑,因?yàn)樗峁┝藘煞N不同的輸出.代碼在 jdk 1.7 上測(cè)試過(guò).

                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 輸出]
                不同的對(duì)象
                有意義的平等.

                [ver1 output]
                Different objects
                Meaningfully equal.

                [ver2 輸出]
                相等對(duì)象
                有意義的平等

                [ver2 output]
                Equal Object
                Meaningfully equal

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

                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 之間的值緩存整數(shù),因此 Integer i = 127 將始終返回相同的引用.Integer j = 128 不一定會(huì)這樣做.然后,您將需要使用 equals 來(lái)測(cè)試底層 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 語(yǔ)言規(guī)范:

                如果被裝箱的值 p 是真、假、一個(gè)字節(jié)或 u0000 到 u007f 范圍內(nèi)的一個(gè)字符,或者一個(gè)介于 -128 和 127(包括)之間的 int 或短數(shù)字,則令 r1 和 r2 為p 的任意兩次裝箱轉(zhuǎn)換的結(jié)果.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.

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

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

                例如,內(nèi)存限制較少的實(shí)現(xiàn)可能會(huì)緩存所有 char 和 short 值,以及 -32K 到 +32K 范圍內(nèi)的 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.

                這篇關(guān)于!= 和 == 運(yùn)算符如何處理 Java 中的整數(shù)?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

                相關(guān)文檔推薦

                How can I detect integer overflow on 32 bits int?(如何檢測(cè) 32 位 int 上的整數(shù)溢出?)
                Local variables before return statements, does it matter?(return 語(yǔ)句之前的局部變量,這有關(guān)系嗎?)
                How to convert Integer to int?(如何將整數(shù)轉(zhuǎn)換為整數(shù)?)
                How do I create an int array with randomly shuffled numbers in a given range(如何在給定范圍內(nèi)創(chuàng)建一個(gè)隨機(jī)打亂數(shù)字的 int 數(shù)組)
                Inconsistent behavior on java#39;s ==(java的行為不一致==)
                Why is Java able to store 0xff000000 as an int?(為什么 Java 能夠?qū)?0xff000000 存儲(chǔ)為 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>

                          主站蜘蛛池模板: 日韩视频免费 | 午夜影院免费观看 | 亚洲精品视频免费 | 色77777| 欧美网站在线观看 | 狠狠做深爱婷婷久久综合一区 | av片免费| 天天操天天干天天爽 | 黄色一级免费视频 | 亚洲九九九| 成人精品福利 | 久久久噜噜噜 | 一区二区三区免费 | 欧美日韩国产一区二区三区 | 日本精品视频在线观看 | 亚洲天堂视频在线观看 | 人人爽视频| 欧美激情国产精品 | 久久精品国产一区二区 | 国产理论视频 | 日本黄色免费视频 | 日韩欧美国产高清91 | 国产suv精品一区 | 国产福利视频在线 | 国产伦精品一区二区三区免费视频 | 日韩成人精品一区二区 | 伦理一区二区 | 亚洲视频在线观看 | 日韩在线看片 | 久草免费在线视频 | 欧美国产在线视频 | 欧美一级黄色大片 | 日韩欧美在线看 | 全部免费毛片在线播放高潮 | 国产成人免费在线视频 | 中文字幕免费在线 | 国产黄色精品视频 | 欧美一二| 国产精品成人国产乱 | 日韩欧美二区 | 亚洲免费久久 |