一、装箱与拆箱
1、装箱——将基本数据类型变为包装类
2、拆箱——将一个包装类变为基本数据类型
二、包装类应用
字符串变为基本数据类型。
1、Integer类(字符串转int型)
public static int parseInt(String s)throws NumberFormatException
2、Float类(字符串转float型)
public static float parseFloat(String s)throws NumberFormatException
3、字符串变为基本数据类型的示例代码
package lkp;
class Test{
public static void main(String args[]){
String str1 = "30";
String str2 = "30.3";
int x = Integer.parseInt(str1);//将字符串转为int型
float f = Float.parseFloat(str2);//将字符串转为float型
System.out.println("整数的乘方" + x + "*" + x+ "=" + (x*x));
System.out.println("小数的乘方" + f + "*" + f + "=" + (f*f));
}
}