1 public class demo { 2 3 public static void main(String[] args) { 4 5 String s="10"; 6 7 //String是字符串数据类型;s是变量;10是字符串 8 9 int a =Integer.parseInt(s); 10 11 /*因类型不匹配,不能从 String 转换为 int, 12 * 13 * 则需要使用封装类,将String字符类型数据转换为Integer整型数据; 14 */ 15 16 System.out.println("变量a的内容是"+a); 17 18 //使用“+”进行字符串和变量a的拼接 19 20 s ="20"; 21 22 //变量s重新赋值 23 24 System.out.println("重新赋值后变量a的内容是:"+s); 25 26 27 } 28 29 }
输出结果为
变量a的内容是10 重新赋值后变量a的内容是:20