数字转字符串:
1.使用String的valueOf()方法
2.先将基本类型转换为封装类型Integer、Float(对象),再使用封装类型的toString()方法
字符串转数字:
使用封装类型的静态方法parseInt、parseFloat
public class Test1 {
public static void main(String[] args) {
//数字转字符串
int n = 100;
//1.使用String的valueOf()方法
String s = String.valueOf(n);
System.out.println(s);
//2.先将int类型转换为int的封装类型Integer(对象),再使用封装类型的toString()方法
Integer m = n;
String s1 = m.toString();
System.out.println(s1);
float a = 3.14f;
//1.使用String的valueOf()方法
String s2 = String.valueOf(a);
System.out.println(s2);
//2.先将int类型转换为float的封装类型Float(对象),再使用封装类型的toString()方法
Float b = a;
String s3 = b.toString();
System.out.println(s3);
//字符串转数字
//Integer的静态方法parseInt
String s4 = "1000";
int c = Integer.parseInt(s4);
System.out.println(c);
//Float的静态方法parseFloat
String s5 = "3.1415";
float e = Float.parseFloat(s5);
System.out.println(e);
}
}