1.基本类型-------》字符串 (以int型为例)
public void BasicType2String(){
int a = 2;
//方法①:将基本类型封装为对象,再调用toString()方法
String str1 = new Integer(a).toString();
//方法②:用空字符串加上基本类型数据
String str2 = "" + a;
//方法③:使用String的valueOf()方法
String str3 = String.valueOf(a);
}
2.字符串--------》基本类型 (以int型为例)
public void String2BasicType(){
String a = "2";
//方法①:用字符串构造基本类型的封装对象,再调用对象的xxxValue()方法
int i1 = new Integer(a).intValue();
//方法②:调用基本类型封装类的parseXxx方法
int i2 = Integer.parseInt(a);
}