In Java, Integer.valueOf() returns an Integer object. You can give it an int, or a String that it parses, optionally with a radix.
The int form reuses cached objects for values from -128 to 127, so two calls with the same small number return the very same object. That is why you should compare Integer objects with equals(), not ==.
A static method of java.lang.Integer: call it on the class, as Integer.valueOf(...). Integer lives in java.lang, so no import is needed.
Syntax
Pass an int, or a String with an optional radix.
Java
Integer a = Integer.valueOf(42); // from an int
Integer b = Integer.valueOf("42"); // from a String
Integer c = Integer.valueOf("ff", 16); // 255, from a String in base 16
Call
What it does
Integer.valueOf(int i)
Boxes an int; small values come from a shared cache.
Integer.valueOf(String s)
Parses text, like parseInt, and boxes the result.
Integer.valueOf(String s, int radix)
Parses text in the given base.
Integer.parseInt(s)
The same parsing, but returns a primitive int.
Autoboxing (Integer x = 5;) calls valueOf(int) for you. new Integer(5) still compiles in Java 8 but is deprecated from Java 9, so prefer valueOf.
Library declarations
Java
// java.lang.Integer
public static Integer valueOf(int i)
public static Integer valueOf(String s) throws NumberFormatException
public static Integer valueOf(String s, int radix) throws NumberFormatException
Parameters
Parameter
Description
i (int)
The number to box.
s (String)
The text to parse. It follows the same rules as parseInt.
radix (int)
The base of the text, from 2 to 36. Optional; the default is 10.
Return Value
Return Value
Description
Integer
An Integer object holding the value. The int form may return a cached, shared object.
Trailing space fails. The text "12 " is not a clean number, so the String form throws NumberFormatException.
A char is widened.'A' is a char, so Java picks valueOf(int) and uses its character code, 65.
A String is parsed."7" goes to the String form and becomes 7.
The fix for digits. To turn the character '7' into the number 7, convert it with String.valueOf first.
Frequently Asked Questions
It returns an Integer object. You can pass an int, or a String (with an optional radix) that it parses.
parseInt returns a primitive int, while valueOf returns an Integer object. Both parse text with the same rules and throw the same NumberFormatException.
valueOf caches objects for -128 to 127, so those values share one object. Larger values create new objects, and == compares references. Use equals().
Use valueOf(5), or just autoboxing. new Integer(5) always makes a new object and is deprecated starting in Java 9.
A char widens to int, so Java calls valueOf(int) with the character code. To parse a digit character, convert it to a String first.