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

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

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

范例:装箱与超想操作

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;

	}
}

  

相关文章:

  • 2021-08-21
  • 2021-07-01
  • 2021-08-18
猜你喜欢
  • 2021-06-23
相关资源
相似解决方案