
大家好,又见面了,我是你们的朋友全栈君。
// String change int public static void main(String[] args) { String str = “123”; int n; // first method // n = Integer.parseInt(str); n = 0; n = Integer.parseInt(str); System.out.println(“Integer.parseInt(str):”+ n); System.out.println(“\n”); //second method //n = Integer.valueOf(str).intValue();
n = 0; n = Integer.valueOf(str).intValue(); System.out.println(“Integer.parseInt(str):”+ n); }

String 转化为 int 时,String必须为整形,否则会报错。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/128710.html原文链接:https://javaforall.cn