紫风乱写

除了他眼前的屏幕,这个人什么也没看见。
被周围的电脑簇拥着,他只知道他所创造的现实,但又意识到那是虚幻。
他已经超越了技术。也超越了机器。
posts - 62, comments - 93, trackbacks - 0, articles - 0
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

基本语法: Sample of autoboxing and unboxing

Posted on 2005-09-11 02:07 Justfly Shi 阅读(572) 评论(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);
  }


}


只有注册用户登录后才能发表评论。


网站导航: