装箱指从基本类型转换为对应的包装类对象;
//装箱 Integer a=new Integer(4); Integer b=Integer.valueOf(45);
拆箱指从包装类对象转换为对应的基本类型 ;
//拆箱 int sum=a.intValue();字符串与基本类型转换
基本类型转换为字符串:
int a=112; String p=a+"";
字符串转换为基本类型:
格式为:类型 变量名=包装类.parseXXX( );
float c=Float.parseFloat("123");
例子:
package start;
public class 包装类 {
public static void main(String[] args) {
//装箱
Integer a=new Integer(4);
Integer b=Integer.valueOf(45);
//拆箱
int sum=a.intValue();
System.out.println("a:"+a);
System.out.println("b:"+b);
System.out.println("sum:"+sum);
System.out.println("...........");
//字符串与基本类型转换;
String p=a+"";
float c=Float.parseFloat("123");
System.out.println(p);
System.out.println(c);
}
}
结果为



