String类型转换为int类型
参考:https://blog.csdn.net/qq_35995940/article/details/78433404?locationNum=5&fps=1
例:
1 public class StringToInt { 2 public static void main(String[] args) { 3 String str = "1313"; 4 int i = 0; 5 6 // eg 1 7 // try { 8 // i = Integer.parseInt(str); 9 // } catch (NumberFormatException e) { 10 // e.printStackTrace(); 11 // } 12 13 // eg 2 14 try { 15 i = Integer.valueOf(str).intValue(); 16 } catch (NumberFormatException e) { 17 e.printStackTrace(); 18 } 19 20 i += 1; 21 System.out.println(i); 22 } 23 }
int类型转换为String类型
例:
1 public class intToString { 2 public static void main(String[] args) { 3 int i = 1314; 4 5 // eg 1 6 // String str = String.valueOf(i); 7 8 // eg 2 9 // String str = Integer.toString(i); 10 11 // eg 3 12 String str = "" + i; 13 14 System.out.println(str); 15 } 16 }