<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Just Java IT

    西門町學士關于Java的隨便一說而已……

    Tiger pitfall

    在SDN踩到一個Tiger的pitfall:
    package sdn;

    import java.util.ArrayList;

    public class BoxingEquality {
       
        /** Creates a new instance of BoxingEquality */
        public BoxingEquality() {
        }
       
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here
            int i = 1;
            int j = 1;
            ArrayList<Integer> list = new ArrayList<Integer>();
            list.add(i);
            list.add(j);
            System.out.printf("It is %b that i == j.\n", (i==j));
            System.out.printf("It is %b that list.get(0) == list.get(1).\n", (list.get(0)==list.get(1)));
            System.out.printf("It is %b that list.get(0).equals(list.get(1)).",            list.get(0).equals(list.get(1)));
        } 
    }

    輸出結果出乎意料:
    It is true that i == j.
    It is true that list.get(0) == list.get(1).    // WHY???
    It is true that list.get(0).equals(list.get(1)).

    然而,僅僅將 i 和 j 的值改成1000:
    int i = 1000;
    int j = 1000;
    就這一個改動后輸出結果雖然符合了java的思維方式,但在這個context中卻更顯得怪異:
    It is true that i == j.
    It is false that list.get(0) == list.get(1). // Oops
    It is true that list.get(0).equals(list.get(1)).

    Sun給出的解釋:
    The primitives are equal and the values of the boxed ints are equal. But this time the ints point to different objects. What you have discovered is that for small integral values, the objects are cached in a pool much like Strings. When i and j are 2, a single object is referenced from two different locations. When i and j are 2000, two separate objects are referenced. Autoboxing is guaranteed to return the same object for integral values in the range [-128, 127], but an implementation may, at its discretion, cache values outside of that range. It would be bad style to rely on this caching in your code.

    呵呵,要不注意說不定還真中招了!

    posted on 2006-03-16 22:03 西門町學士 閱讀(333) 評論(4)  編輯  收藏 所屬分類: Java

    Feedback

    # re: Tiger pitfall 2006-10-21 09:56 11

    11111111111111111  回復  更多評論   

    # re: Tiger pitfall 2006-10-21 09:58 11

    不好意思!剛才發錯了!  回復  更多評論   

    # 你好! 2006-10-21 10:04 11

    由于最近在做一個在線購物系統!所以看到你的主頁后就來想想你這個留言版是怎么實現的~!:)  回復  更多評論   

    # 關于你的留言版的實現原理 2006-10-21 10:07 11

    由于最近在做一個在線購物系統!所以看到你的主頁后就來想想你這個留言版是怎么實現的~!:)   回復  更多評論   

    主站蜘蛛池模板: 国产成人亚洲精品91专区高清| 亚洲福利秒拍一区二区| 无套内射无矿码免费看黄| 成人免费午夜视频| 亚洲中文字幕无码久久| A级毛片内射免费视频| 亚洲国产精品无码中文lv| 日韩中文无码有码免费视频| 亚洲AV日韩综合一区| 国产免费啪嗒啪嗒视频看看| 特级一级毛片免费看| 亚洲一级特黄无码片| 国产日韩一区二区三免费高清| 亚洲AV电影院在线观看| 99精品视频在线视频免费观看 | 亚洲av日韩片在线观看| 羞羞视频在线观看免费| 亚洲色欲久久久综合网| 久久精品视频免费| 亚洲欧洲日本天天堂在线观看| 女人让男人免费桶爽30分钟| 曰批全过程免费视频免费看 | 成人亚洲网站www在线观看| 日韩免费码中文在线观看| 亚洲中久无码永久在线观看同| 久别的草原电视剧免费观看| 亚洲激情视频图片| 亚洲av无码成人精品区| 久久免费国产精品一区二区| 亚洲伊人久久大香线蕉啊| 免费99热在线观看| 在线观看免费无码专区| 亚洲一区二区三区乱码在线欧洲| 亚洲国产成人精品女人久久久 | 亚洲国产主播精品极品网红| 日本免费一区二区三区| 黑人粗长大战亚洲女2021国产精品成人免费视频 | 亚洲人成电影在线播放| 2019中文字幕免费电影在线播放| 亚洲精华液一二三产区| 亚洲av丰满熟妇在线播放|