将String类型转换为int整数类型

别来无恙 提交于 2019-12-02 02:09:38

 

 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

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!