紫風亂寫

          除了他眼前的屏幕,這個人什么也沒看見。
          被周圍的電腦簇擁著,他只知道他所創造的現實,但又意識到那是虛幻。
          他已經超越了技術。也超越了機器。
          posts - 62, comments - 93, trackbacks - 0, articles - 0
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          基本語法: Sample of autoboxing and unboxing

          Posted on 2005-09-11 02:07 Justfly Shi 閱讀(592) 評論(0)  編輯  收藏 所屬分類: Study Tiger

          要注意的是,當要被unboxing的封裝類為null的時候或未被初始化時會拋出一個nullpoint錯誤

          package cn.justfly.study.tiger;

          import java.util.ArrayList;
          import java.util.Collection;

          /**
           * Sample code of autoBoxing and auto-unboxing 
           * @author Justfly Shi
           * created at 2005-8-28 
           
          */

          public class Boxing {

            
          /**
             * @param args
             
          */

            
          public static void main(String[] args) {
              Collection
          <Integer> c = new ArrayList<Integer>();

              
          for (int i = 0; i < 5; i++{
                c.add(i);
          // autoboxing
              }

              System.
          out.println("iterate with Interger");
              
          for (Integer i : c) {

                System.
          out.println(i + 100);// unboxing
              }


              System.
          out.println("iterate with int");
              
          for (int i : c)// unboxing in enhanced for
              {

                System.
          out.println(i + 100);
              }

              
              Integer i
          =null;
              
          int j=i;//NullPointerException will be throw here
              System.out.println(j);
            }


          }

          主站蜘蛛池模板: 龙南县| 施甸县| 柯坪县| 高州市| 曲麻莱县| 东莞市| 汤原县| 鲁山县| 黄大仙区| 溧阳市| 从江县| 抚顺市| 岳西县| 巴林左旗| 浠水县| 胶南市| 青田县| 社旗县| 灵宝市| 邵武市| 柳林县| 阿合奇县| 曲麻莱县| 岑溪市| 松原市| 保山市| 莱西市| 攀枝花市| 兖州市| 河间市| 德格县| 太保市| 龙口市| 宜宾县| 潍坊市| 澄迈县| 杭锦旗| 凤阳县| 天门市| 商水县| 屏南县|