public class Test1 {
public static void main(String[] args) {
String str = "1";
try {
int i = Integer.parseInt(str);
System.out.println(i);
} catch (NumberFormatException e) {
e.printStackTrace();
}
System.out.println("******");
String str1 = "2";
try {
int i1 = Integer.valueOf(str1).intValue();
System.out.println(i1);
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
}
在转换过程中需要注意,因为字符串中可能会出现非数字的情况,所以在转换的时候需要捕捉处理异常