在Java中若需要将String类型转为int类型,该使用什么方法呢?主要有以下几种方法可以使用,读者可按需使用。
1 Integer.parseInt(String)方法,该方法会返回字符串中的原始整数
具体实例:
String s="103";
int x=Integer.parseInt(s);
System.out.println(x);
//输出:103
2 Integer.valueOf(String)方法,该方法返回的是一个Integer对象
具体实例:
String s="-209";
int x=Integer.valueOf(s);
System.out.println(x);
//输出:-209
注意:以上两种方法使用时,待转换的字符串必须为纯数字,若字符串中有其他字符则会抛出NumberFormatException异常
本文介绍了在Java中如何将String类型转换为int类型的方法,包括使用Integer.parseInt()和Integer.valueOf()两种方式,并提供了具体的代码示例。
5541

被折叠的 条评论
为什么被折叠?



