Every wrapper class in java has 3 forms of valueOf method. They are,

  1. A valueOf() method which takes primitive type as an argument
  2. A valueOf() method which takes String type as an argument
  3. A valueOf() method which takes two arguments. One is String type and another one is int type.

The valueOf() methods are mainly used to wrap or box the primitive content into wrapper class objects.


A valueOf() method with primitive type as an argument
This form of valueOf method takes primitive type data as an argument and returns corresponding wrapper class object. The template of this form looks like :

public static return_type valueOf ( primitive_type )

where return_type is any Wrapper Class.

Following example shows the usage of this form of valueOf() method.

public class WrapperClasses
{
    public static void main(String[] args)
    {
        Byte B = Byte.valueOf((byte) 123);     //Output : 123
        Short S = Short.valueOf((short) 25);   //Output : 25
        Integer I = Integer.valueOf(46);       //Output : 46
        Long L = Long.valueOf(235);            //Output : 235
        Float F = Float.valueOf(23.5f);        //Output : 23.5
        Double D = Double.valueOf(15.4d);      //Output : 15.4
        Boolean BLN = Boolean.valueOf(true);   // Output : true
        Character C = Character.valueOf('C');  // Output : C
    }
}


valueOf() Method with string as an argument
This form of valueOf method takes string as an argument and returns corresponding wrapper class object. It throws NumberFormatException, if string is not a valid numeric value. Character wrapper class doesn’t have this method as string can not be converted to character. The template of this form looks like :

public static return_type valueOf(String s) throws NumberFormatException

where return_type is any wrapper class.

Example:

Short S = Short.valueOf("25");


valueOf() Method with string and int as an arguments
This form of valueOf method takes two arguments. One is String type which holds valid numeric value to be converted into wrapper class object and second argument is int type which indicates the radix or base of that numeric value. This form also throws NumberFormatException if String is not a valid numeric value. This method is available only in Byte, Short, Integer and Long wrapper classes. The template of this form is,

public static return_type valueOf(String s, int radix) throws NumberFormatException

where return_type is any wrapper class.

Example:

// Number with base 12 is converted into decimal value
Integer I = Integer.valueOf("4673AB", 12);
System.out.println(I);            //Output : 1132403