Table of Contents

    Java valueOf() Method: Usage and Examples

    Description:

    The valueOf method returns the relevant Number Object holding the value of the argument passed. The argument can be a primitive data type, String, etc.

    This method is a static method. The method can take two arguments, where one is a String and the other is a radix.


    Syntax:

    All the variants of this method are given below:

    static Integer valueOf(int i)
    static Integer valueOf(String s)
    static Integer valueOf(String s, int radix)

    The static valueOf method can be used to convert an array of characters into a string. There are several overloaded versions of the valueOf method that can be used to convert a charac- ter and numeric values to strings with different parameter types, char , double , long , int , and float

    Methods Descriptions
    +valueOf(c: char): String Returns a string consisting of the character c.
    +valueOf(data: char[]): String Returns a string consisting of the characters in the array.
    +valueOf(d: double): String Returns a string representing the double value.
    +valueOf(f: float): String Returns a string representing the float value.
    +valueOf(i: int): String Returns a string representing the int value.
    +valueOf(l: long): String Returns a string representing the long value.
    +valueOf(b: boolean): String  Returns a string representing the boolean value.

    Parameters:

    Here is the detail of parameters:

    • i -- An int for which Integer representation would be returned.

    • s -- A String for which Integer representation would be returned.

    • radix -- This would be used to decide the value of returned Integer based on passed String.


    Return Value:

    • valueOf(int i): This returns an Integer object holding the value of the specified primitive.

    • valueOf(String s): This returns an Integer object holding the value of the specified string representation.

    • valueOf(String s, int radix): This returns an Integer object holding the integer value of the specified string representation, parsed with the value of radix.

    public class Test{ 
    
       public static void main(String args[]){
          
          Integer x =Integer.valueOf(9);
          Double c = Double.valueOf(5);
          Float a = Float.valueOf("80");               
    
          Integer b = Integer.valueOf("444",16);
    
          System.out.println(x); 
          System.out.println(c);
          System.out.println(a);
          System.out.println(b);
       }
    }

    This produces the following result:

    9
    5.0
    80.0
    1092
    Methods Descriptions
    static Float valueOf(float num ) Returns a Float object containing the value passed in num.
    static Float valueOf(String str )
    throws NumberFormatException
    Returns the Float object that contains the value specified
    by the string in str.
    static Double valueOf(double num  Returns a Double object containing the value passed
    in num.
    static Double valueOf(String str )
    throws NumberFormatException
    Returns a Double object that contains the value
    specified by the string in str.
    static Byte valueOf(byte num )  Returns a Byte object containing the value passed
    in num.
    static Byte valueOf(String str )
    throws NumberFormatException
    Returns a Byte object that contains the value
    specified by the string in str.
    static Byte valueOf(String str , int radix )
    throws NumberFormatException
    Returns a Byte object that contains the value
    specified by the string in str using the specified radix.
    static Short valueOf(short num ) Returns a Short object containing the value passed
    in num.
    static Short valueOf(String str )
    throws NumberFormatException
    Returns a Short object that contains the value
    specified by the string in str using radix 10.
    static Short valueOf(String str , int radix )
    throws NumberFormatException
    Returns a Short object that contains the value
    specified by the string in str using the specified radix.
    static Integer valueOf(int num  Returns an Integer object containing the value passed
    in num.
    static Integer valueOf(String str )
    throws NumberFormatException
    Returns an Integer object that contains the value
    specified by the string in str.
    static Integer valueOf(String str , int radix )
    throws NumberFormatException
    Returns an Integer object that contains the value
    specified by the string in str using the specified radix.
    static Long valueOf(long num Returns a Long object containing the value passed
    in num.
    static Long valueOf(String str )
    throws NumberFormatException
    Returns a Long object that contains the value
    specified by the string in str.
    static Long valueOf(String str , int radix )
    throws NumberFormatException
    Returns a Long object that contains the value
    specified by the string in str using the specified radix.
    static Boolean valueOf(boolean boolVal  Returns the Boolean equivalent of boolVal
    static Boolean valueOf(String boolString  Returns true if boolString contains the string “true” (in
    uppercase or lowercase). Otherwise, it returns false.
    static > T
    valueOf(Class e-type , String name )
    Returns the constant associated with name in the
    enumeration type specified by e-type.

    Example

    // Java program to demonstrate valueOf() method
    public class ValueOfMethod
    {
        public static void main(String[] args)
        {
            // demonstrating valueOf(int i) method
            System.out.println("Demonstrating valueOf(int i) method");
            Integer i =Integer.valueOf(50);
            Double d = Double.valueOf(9.36);
            System.out.println(i);
            System.out.println(d);
    
            // demonstrating valueOf(String s) method
            System.out.println("Demonstrating valueOf(String s) method");
            Integer n = Integer.valueOf("333");
            Integer m = Integer.valueOf("-255");
            System.out.println(n);
            System.out.println(m);
    
            // demonstrating valueOf(String s,int radix) method
            System.out.println("Demonstrating (String s,int radix) method");
            Integer y = Integer.valueOf("333",8);
            Integer x = Integer.valueOf("-255",16);
            Long l = Long.valueOf("51688245",16);
            System.out.println(y);
            System.out.println(x);
            System.out.println(l);
    
            // run-time NumberFormatException will occur in below cases
            Integer a = Integer.valueOf("Hello");
            Integer b = Integer.valueOf("Hello",16);
        }
    }

    This will produce the following result ?

    Output

    Demonstrating valueOf(int i) method
    50
    9.36
    Demonstrating valueOf(String s) method
    333
    -255
    Demonstrating (String s,int radix) method
    219
    -597
    1365803589
    Exception in thread "main" java.lang.NumberFormatException: For input string: "Hello"
            at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
            at java.lang.Integer.parseInt(Integer.java:580)
            at java.lang.Integer.valueOf(Integer.java:766)
            at ValueOfMethod.main(ValueOfMethod.java:30)
    Press any key to continue . . .