Java Integer valueOf() Method
Example
Create Integer objects from an int and from a string:
public class Main {
public static void main(String[] args) {
Integer a = Integer.valueOf(5);
Integer b = Integer.valueOf("10");
System.out.println(a + b);
}
}
Definition and Usage
The valueOf() method returns an Integer object. You can give it an int, or a string that contains a whole number.
It works like parseInt(), but parseInt() returns a primitive int and valueOf() returns an Integer object.
Syntax
One of the following:
public static Integer valueOf(int i)
public static Integer valueOf(String s)
public static Integer valueOf(String s, int radix)
Parameter Values
| Parameter | Description |
|---|---|
| i | An int value to store in the object. Used in the first version. |
| s | A string with a whole number, like "25". Used in the second and third versions. |
| radix | Optional. The base of the number system (the radix), for example 2 for binary or 16 for hexadecimal. The default is 10. |
Technical Details
| Returns: | An Integer object with the value. |
|---|---|
| Throws: | NumberFormatException - if the string is not a whole number, or the number is too large for an int. |
Related Pages
❮ Integer Methods