Java valueOf() Method
The valueOf() method returns the native Number object value of the given parameter, which can be a primitive data type, String, etc.
This method is a static method. It can take two parameters: one is a string and the other is a radix.
Syntax
This method has the following syntax formats:
static Integer valueOf(int i)
static Integer valueOf(String s)
static Integer valueOf(String s, int radix)
Parameters
-
i -- Integer object's integer.
-
s -- Integer object's string.
-
radix -- The radix used in parsing string s, specifying the base number system.
Return Value
-
Integer valueOf(int i): Returns an Integer instance representing the specified int value.
-
Integer valueOf(String s): Returns an Integer object holding the value of the specified String.
-
Integer valueOf(String s, int radix): Returns an Integer object holding the value extracted from the specified String when parsed using the radix provided by the second parameter.
Example
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); // Using base 16
System.out.println(x);
System.out.println(c);
System.out.println(a);
System.out.println(b);
}
}
Compiling the above program will output the following:
9
5.0
80.0
1092