1、基本类型和包装类
基本类型和包装类可通过自动装箱和拆箱实现。
int i = 24;
Integer a = new Integer(i); //手动装箱
Integer b = i; //自动装箱
int x = a; //自动拆箱
int y = a.intValue(); //受到拆箱
2、基本类型转String
a.使用包装类的toString方法
int i =24;
String str1 = Integer.toString(i);
b.使用String类的valueOf方法
String str2 = String.valueOf(i);
c.基本类型加空字符串
String str3 = i + “”;
3、String转基本类型
a.调用包装类的静态方法parseXxx,返回值为基本类型
String str = “24”;
int a = Integer.parseInt(str);
b.调用包装类的静态方法valueOf,返回值为包装类
int b = Integer.valueOf(str);
public void test1() {
//包装类型和基本类型互转
Integer i = new Integer(100);
int ii=i.intValue();
//包装类型和String类型互转
Integer iii=new Integer("1000");
String stri=iii.toString();
//基本类型和String类型互转
int i2=Integer.parseInt("1000");
//int i3=Integer.valueOf("200");
String sst=i2+"";
String sst1=String.valueOf(i2);
}