字符串转换为整数:
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);
}
如果输入的字符串中不是有效的整数时,会出现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)
整数转为字符串:
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);
}