valueof() in Java

The valueOf() is a static method present in Java it’s primary purpose is to convert primitive data types into their wrapper class object.

valueOf() Form 1

Every wrapper class except the character class contains valueOf() to create a wrapper class object for a given String.

ublic class WrapperClassInJava {
    public static void main(String[] args) {
       Integer i = Integer.valueOf("12");
       Double d = Double.valueOf("12.5");
    }
}

valueOf() Form 2

Every integral type wrapper class(Byte, Short, Integer, Long) contains the following valueOf() to create a wrapper class object for the given specified radix String.

public static Integer valueOf(     @NotNull  String s,
 int radix )

The allowed range of radix is 2 to36

public class WrapperClassInJava {
    public static void main(String[] args) {
       Integer i = Integer.valueOf("100",2);
        System.out.println(i);
    }
}
valueof()

valueOf() Form 3

Every wrapper class including the character class contains a valueOf() to create a wrapper object for a given primitive value.

public class WrapperClassInJava {
    public static void main(String[] args) {
     Character ch = Character.valueOf('a');
     Integer i = Integer.valueOf(23);
    }
}

Similar Java Tutorials

Leave a Comment