1.普通类型 –> 字符串类型
有多种方式转换为字符串
int a = 2;
String s1 = a + ""; //使用最简单的 +
int b = 2;
Integer b2 = b;
String s2 = b2.toString(); //使用包装类的 toString 方法
int c = 2;
String s3 = String.valueOf(c); //使用 String.valueOf 方法
2.字符串 –> 基本类型
除了 Character 类型以外,包装类均有 parseXXX 方法转换为对应的基本类型
String s = "2";
byte a = Byte.parseByte(s); // byte
short b = Short.parseShort(s); //short
int c = Integer.parseInt(s); //int
Long d = Long.parseLong(s); //long
float e = Float.parseFloat(s); //float
double f = Double.parseDouble(s); //double
boolean g = Boolean.parseBoolean(s); //boolean
无法直接转化为 char ,可以直接转化为’字符数组’,或取单字符
String s = "abc";
char[] chars = s.toCharArray(); //转化为字符数组
char c = s.charAt(1); //直接取某个下标