Java Integer类valueOf()方法及示例

Syntax:

句法:

    public static Integer valueOf (int value);
    public static Integer valueOf (String value);
    public static Integer valueOf (String value, int radix's);

整数类valueOf()方法 (Integer class valueOf() method)

  • valueOf() method is available in java.lang package.

    valueOf()方法在java.lang包中可用。

  • valueOf (int value) method is used to represent the Integer object denoted by the given argument (value) is of int type.

    valueOf(int值)方法用于表示由给定参数(值)表示的Integer对象为int类型。

  • valueOf (String value) method is used to represent the Integer object holding the integer value denoted by the given argument (value) is of String type.

    valueOf(字符串值)方法用于表示Integer对象,该对象持有由给定参数(值)表示的整数值是String类型。

  • valueOf (String value, int radix's) method is used to represent an Integer object holding the integer value of the given argument (value) in the radix's given by the second argument.

    valueOf(String value,int radix's)方法用于表示一个Integer对象,该对象在第二个参数给出的基数中保存给定参数(值)的整数值。

  • valueOf (int value) method does not throw an exception at the time of returning an Integer instance.

    返回Integer实例时, valueOf(int值)方法不会引发异常。

  • Similarly, valueOf (String value), valueOf (String value, int radix's) method may throw a NumberFormatException at the time of returning an instance.

    同样,在返回实例时, valueOf(String value)valueOf(String value,int radix's)方法可能会引发NumberFormatException。

    NumberFormatException: In this exception, if String does not contain the parsable number.

    NumberFormatException:在此异常中,如果String不包含可分析的数字。

  • These methods are static methods, it is accessible with the class name too and, if we try to access these methods with the class object then also we will not get an error.

    这些方法是静态方法,也可以使用类名进行访问,而且,如果尝试使用类对象访问这些方法,那么也不会出错。

Parameter(s):

参数:

  • In the first case, int value - This parameter represent the value of int type.

    在第一种情况下, int value-此参数表示int类型的值。

  • In the second case, String value - This parameter represents the value of String type.

    在第二种情况下, 字符串值 -此参数表示字符串类型的值。

  • In the third case, String value, int radix's - In this method first parameter value represents the value of String type in the radix's given by the second parameter.

    在第三种情况下, String值,int radix's-在此方法中,第一个参数值表示第二个参数给定的基数中String类型的值。

Return value:

返回值:

In the first case, the return type of this method is Integer - it returns the Integer representation of this integer argument.

在第一种情况下,此方法的返回类型为Integer-它返回此整数参数的Integer表示形式。

In the second case, the return type of this method is Integer - it returns the Integer representation of this String argument.

在第二种情况下,此方法的返回类型为Integer-它返回此String参数的Integer表示形式。

In the third case, the return type of this method is Integer - it returns the Integer representation of the string argument in the radix’s given by the second argument.

在第三种情况下,此方法的返回类型为Integer-它以第二个参数给定的基数形式返回字符串参数的Integer表示形式。

Example:

例:

// Java program to demonstrate the example 
// of valueOf() method of Integer class

public class ValueOfIntegerClass {
    public static void main(String[] args) {
        // Object initialization
        Integer ob1 = new Integer(100);
        Integer ob2 = new Integer(200);
        Integer ob3 = new Integer(40);

        // Display ob1,ob2,ob3 values
        System.out.println("ob1:" + ob1);
        System.out.println("ob2:" + ob2);
        System.out.println("ob3:" + ob3);

        // It returns Integer object holding the value 
        // denoted by the given integer argument
        Integer value1 = ob1.valueOf(20);

        // String object initialization for valueOf(String s)
        String s = "80";

        // It returns Integer object holding the value 
        // denoted by the given String argument
        Integer value2 = ob2.valueOf(s);

        // It returns Integer object holding the value 
        // denoted by the given String argument with radix 20
        Integer value3 = ob3.valueOf(s, 20);

        // Display result values
        System.out.println("ob1.valueOf(20.10d): " + value1);
        System.out.println("ob2.valueOf(s): " + value2);
        System.out.println("ob3.valueOf(s,20): " + value3);
    }
}

Output

输出量

ob1:100
ob2:200
ob3:40
ob1.valueOf(20.10d): 20
ob2.valueOf(s): 80
ob3.valueOf(s,20): 160


翻译自: https://www.includehelp.com/java/integer-class-valueof-method-with-example.aspx

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值