幻境
          We are extremely fortunate not to know precisely the kind of world we live in
          posts - 22,comments - 39,trackbacks - 0

           

          原生類型的autoboxingauto-unboxing

          我們知道,在Java中,int,long等原生類型不是一個繼承自Object的類,所以相應的,有很多操作我們都不能利用原生類型操作,比如想要把一個整數放入到一個集合中,我們必須首先創建一個Integer對象,然后再將這個對象放入到集合中。當我們從集合中取數的時候,取出來的是一個Integer對象,因此不能直接對它使用加減乘除等運算符,而是必須用Integer.intValue()取到相應的值才可以,這樣的過程稱之為boxingunboxing

          J2SE5.0支持autoboxingauto-unboxing,也就是說我們以后不需要再手動地做這些boxingunboxing操作了,java語言會替我們完成。具體可以參照下面的示例:

          List<Integer> intList=new ArrayList<Integer>();

          intList.add(2);

          intList.add(new Integer(5));

          int i=3+intList.get(0);//i=5

          int j=3+intList.get(1); //j=8

           

          從這一段程序中我們可以看到,autoboxingauto-unboxing為我們省掉了很多不必要的工作。

          posted on 2005-05-12 10:57 閱讀(1212) 評論(1)  編輯  收藏 所屬分類: 編程相關

          FeedBack:
          # re: J2SE5.0新特性示例---原生類型的autoboxing和auto-unboxing
          2006-07-28 15:35 | jclue
          say if I add another int to the intList like

          intList.add(3); // index 2

          now we have {2 ,5 ,3} in the intList

          what happen if do intList.remove (2) ???
          would the 3 (index 2) be removed or 2 (index 0)?
            回復  更多評論
            
          主站蜘蛛池模板: 曲麻莱县| 金阳县| 平遥县| 弥渡县| 霍邱县| 禹城市| 汉阴县| 社会| 察哈| 石首市| 长兴县| 郧西县| 怀仁县| 嘉义县| 越西县| 永吉县| 文化| 长寿区| 日照市| 买车| 普宁市| 尉氏县| 体育| 常山县| 云林县| 乐安县| 肃北| 鹤峰县| 赫章县| 抚顺市| 鄱阳县| 莒南县| 江口县| 红原县| 黔东| 三原县| 奎屯市| 称多县| 宣威市| 澄江县| 新兴县|