Integer.parseInt()

在对字符串进行操作时,有时我们需要将表示为字符串的数字转换为整数类型。Java中一般用于将 String 转换为 Integer 的方法是parseInt()。该方法属于 java.lang 包中的 Integer 类。它将一个有效字符串作为参数并将其解析为原始数据类型 int。它只接受 String 作为参数,并且在传递任何其他数据类型的值时,由于类型不兼容,它会产生错误。

此方法有两种变体:

语法:

public static int parseInt(String s) throws NumberFormatException
public static int parseInt(String s, int radix) throws NumberFormatException

示例代码:

// Java program to demonstrate working parseInt()

public class YiibaiDemo {
    public static void main(String args[])
    {
        int decimalExample = Integer.parseInt("20");
        int signedPositiveExample = Integer.parseInt("+20");
        int signedNegativeExample = Integer.parseInt("-20");
        int radixExample = Integer.parseInt("20", 16);
        int stringExample = Integer.parseInt("geeks", 29);

        System.out.println(decimalExample);
        System.out.println(signedPositiveExample);
        System.out.println(signedNegativeExample);
        System.out.println(radixExample);
        System.out.println(stringExample);
    }
}

运行结果如下:

20
20
-20
32
11670324

Integer.valueOf()

此方法是属于 java.lang 包的静态方法,它返回相关的整数对象,其中包含传递的参数的值。此方法可以将整数或字符串作为参数。但是当给定的字符串无效时,它会提供一个错误。此方法也可以将字符作为参数,但输出将是其对应的 Unicode 值。此方法将始终缓存 -128127(含)范围内的值,并且可能缓存此范围之外的其他值。

语法:

public static Integer valueOf(int a)
public static Integer valueOf(String str)
public static Integer valueOf(String str, int base)

示例:

// Java program to illustrate the
// java.lang.Integer.valueOf(int a)
import java.lang.*;

public class Geeks {
    public static void main(String[] args)
    {
        Integer obj = new Integer(10);
        // Returns an Integer instance
        // representing the specified int value
        System.out.println("Output Value = " + obj.valueOf(85));
    }
}

运行结果:

Output Value = 85

Integer.parseInt() 和 Integer.valueOf() 的区别

区别1: Integer.valueOf() 返回一个 Integer 对象,而 Integer.parseInt() 返回一个原始 int
示例:

// Program to show the use
// of Integer.parseInt() method

class Test1 {
    public static void main(String args[])
    {
        String s = "7788";

        // Primitive int is returned
        int str = Integer.parseInt(s);
        System.out.println("str=>"+str);
        // Integer object is returned
        int str1 = Integer.valueOf(s);
        System.out.println("str1=>"+str1);
    }
}

运行结果:

str=>7788
str1=>7788

区别2: String 和 integer 都可以作为参数传递给 Integer.valueOf(),而只有 String 可以作为参数传递给 Integer.parseInt()
示例:

// Program to show that Integer.parseInt()
// cannot take integer as parameter

class Test3 {
    public static void main(String args[])
    {
        int val = 99;

        try {

            // It can take int as a parameter
            int str1 = Integer.valueOf(val);
            System.out.print(str1);

            // It cannot take an int as a parameter
            // Hence will throw an exception
            int str = Integer.parseInt(val);
            System.out.print(str);
        }
        catch (Exception e) {
            System.out.print(e);
        }
    }
}

运行结果(编译错误):

prog.java:18: error: incompatible types:
int cannot be converted to String
            int str = Integer.parseInt(val);
                                       ^
1 error

区别3: Integer.valueOf()可以将字符作为参数并返回其对应的 unicode 值,而 Integer.parseInt() 将在将字符作为参数传递时产生错误。

// Program to test the method
// when a character is passed as a parameter

class Test3 {
    public static void main(String args[])
    {
        char val = 'A';

        try {

            // It can take char as a parameter
            int str1 = Integer.valueOf(val);
            System.out.print(str1);

            // It cannot take char as a parameter
            // Hence will throw an exception
            int str = Integer.parseInt(val);
            System.out.print(str);
        }
        catch (Exception e) {
            System.out.print(e);
        }
    }
}

运行结果:

prog.java:18: error: incompatible types:
char cannot be converted to String
            int str = Integer.parseInt(val);
                                       ^
1 error

Integer.parseInt() 和 Integer.valueOf() 对比和区别

Integer.parseInt() Integer.valueOf()
它只能接受一个字符串作为参数。 它可以接受一个字符串和一个整数作为参数。
它返回一个原始 int 值。 它返回一个Integer对象。
当整数作为参数传递时,由于类型不兼容而产生错误 当整数作为参数传递时,它返回与给定参数对应的 Integer 对象。
当字符作为参数传递时,此方法会产生错误(不兼容的类型)。 该方法可以接受一个字符作为参数,并会返回对应的unicode。
这在性能方面落后了,因为与生成字符串相比,解析字符串需要很多时间。 通过缓存频繁请求的值,此方法可能会显着提高空间和时间性能。
如果需要原始 int 数据类型,则使用 Integer.parseInt() 方法。 如果需要 Wrapper Integer 对象,则使用 valueOf() 方法。