- Java.lang Package classes
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package extras
- Java.lang - Interfaces
- Java.lang - Errors
- Java.lang - Exceptions
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
Java - valueOf() Method
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
Following are all the variants of this method −
static Integer valueOf(int i) static Integer valueOf(String s) static Integer valueOf(String s, int radix)
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 the 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.
Example 1
In this example, we're showing the usage of valueOf(int) and valueOf(double) method to get an Integer/Double objects holding the value of provided int/double, primitive variables. We've created a Integer variable x, Double variable d. Using valueOf(int) and valueOf(double), we're populating x and b and then these variables are printed to verify the result.
public class Test { public static void main(String args[]) { Integer x =Integer.valueOf(9); Double d = Double.valueOf(5); System.out.println(x); System.out.println(d); } }
This will produce the following result −
Output
9 5.0
Example 2
In this example, we're showing the usage of valueOf(String) method to get an Integer/Double objects holding the value of provided int/double, in form of String. We've created a Integer variable x, Double variable d. Using valueOf(String), we're populating x and b and then these variables are printed to verify the result.
public class Test { public static void main(String args[]) { Integer x =Integer.valueOf("9"); Double d = Double.valueOf("5"); System.out.println(x); System.out.println(d); } }
This will produce the following result −
Output
9 5.0
Example 3
In this example, we're showing the usage of valueOf(String, int radix) method to get an Integer object holding the value of provided int, in form of String for a given radix. We've created a Integer variable x. Using valueOf(String, int), we're populating x based on radix 16 and then this variables is printed to verify the result.
public class Test { public static void main(String args[]) { Integer x =Integer.valueOf("A", 16); System.out.println(x); } }
This will produce the following result −
Output
10
To Continue Learning Please Login
Login with Google