//  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);
    }
JAVA String 转换为 int方法

String 转化为 int 时,String必须为整形,否则会报错。

相关文章: