拆箱和装箱

在了解了包装类的概念以后,下面介绍装箱与拆箱

装箱:将一个基本数据类型变为包装类

拆箱:将一个包装类变为基本类型的过程

范例:装箱与超想操作

package test3;

public class test1 {
	public static void main(String[] args) {
		int x = 30;// 声明一个基本数据类型
		Integer i = new Integer(x);// 装箱
		int temp = i.intValue();// 拆箱
	}
}

  

package test3;

public class WraperDemo02 {
	public static void main(String[] args) {
		float f = 30.3f;
		Float x = new Float(f);
		float y = x.floatValue();
	}
}

  JDK1.5之后提供了自动的装箱以及拆箱

范例:自动装箱及拆箱

package test3;

public class WrapperDemo03 {
	public static void main(String[] args) {
		Integer i = 30;// 自动装箱
		Float f = 30.3f;
		int x = i;// 自动拆箱
		float y = f;

	}
}

  

posted on 2011-12-07 15:49  wangbokun  阅读(184)  评论(0编辑  收藏  举报

导航