字符串轉(zhuǎn)換為整數(shù):
public static void main(String[] args) {
String test = "445";
int number = Integer.parseInt(test);
System.out.println(number);
}
public static void main(String[] args) {
String test = "445";
int number = Integer.valueOf(test);
System.out.println(number);
}
如果輸入的字符串中不是有效的整數(shù)時由蘑,會出現(xiàn)java.lang.NumberFormatException:
異常:
public static void main(String[] args) {
String test = "過河";
int number = Integer.valueOf(test);
System.out.println(number);
}
異常信息:
Exception in thread "main" java.lang.NumberFormatException: For input string: "過河"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.valueOf(Integer.java:766)
at com.mmall.util.PropertiesUtil.main(PropertiesUtil.java:41)
整數(shù)轉(zhuǎn)為字符串:
public static void main(String[] args) {
int number = 445;
String test = Integer.toString(number);
System.out.println(test);
}
public static void main(String[] args) {
int number = 445;
String test = String.valueOf(number);
System.out.println(test);
}