基本类型转换为字符串有三种方法:
1. 使用包装类的 toString() 方法
2. 使用String类的 valueOf() 方法
3. 用一个空字符串加上基本类型,得到的就是基本类型数据对应的字符串
1. 使用包装类的 toString() 方法
2. 使用String类的 valueOf() 方法
3. 用一个空字符串加上基本类型,得到的就是基本类型数据对应的字符串
//将基本类型转换为字符串
int str=100;
String str1=Integer.toString(str); //方法一
String str2=String.valueOf(str); //方法二
String str3=str+""; //方法三
int str=100;
String str1=Integer.toString(str); //方法一
String str2=String.valueOf(str); //方法二
String str3=str+""; //方法三
再来看,将字符串转换成基本类型有两种方法:
1. 调用包装类的 parseXxx 静态方法
2. 调用包装类的 valueOf() 方法转换为基本类型的包装类,会自动拆箱
//将字符串转换为基本类型
String s="100";
int a1=Integer.valueOf(s); //方法一
int a2=Integer.parseInt(s); //方法二
String s="100";
int a1=Integer.valueOf(s); //方法一
int a2=Integer.parseInt(s); //方法二