Java Integer valueOf() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Integer

Definition

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
CallWhat 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

ParameterDescription
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 ValueDescription
IntegerAn Integer object holding the value. The int form may return a cached, shared object.

Compare with Similar Methods

Compare with the other ways to get an int:

MethodHow it differs
parseInt(s)Returns a primitive int, with no object created.
decode(s)Parses text that may start with 0x, #, or 0.
intValue()The opposite step: unwraps an Integer to an int.
new Integer(5)Always creates a new object and is deprecated since Java 9.

Java Examples

Example 1 — Create Integer Objects from int and String Values

Build Integer objects from a number, from text, and from text in another base.

Java
public class IntegerValueOfDemo {
    public static void main(String[] args) {
        Integer fromInt = Integer.valueOf(42);
        Integer fromText = Integer.valueOf("42");
        Integer fromHex = Integer.valueOf("2a", 16);

        System.out.println(fromInt + " " + fromText + " " + fromHex);
        System.out.println("all equal: " + (fromInt.equals(fromText) && fromText.equals(fromHex)));
        System.out.println("sum: " + (fromInt + fromText + fromHex));
    }
}

How It Works

  1. Three sources. An int, the text "42", and the hex text "2a" each produce an Integer.
  2. Same value. Hex 2a is 2 × 16 + 10, which is 42, so all three print 42.
  3. equals compares values. The equals calls confirm that the three objects hold the same number.
  4. They unbox in arithmetic. The + operator unwraps each object, so the total is 126.

Example 2 — See Why == Fails for Larger Integer Objects

Compare boxed numbers with == and with equals() on both sides of the cache limit.

Java
public class IntegerValueOfCache {
    public static void main(String[] args) {
        Integer a = Integer.valueOf(127);
        Integer b = Integer.valueOf(127);
        System.out.println("127 == 127:      " + (a == b));
        System.out.println("127 equals 127:  " + a.equals(b));

        Integer c = Integer.valueOf(128);
        Integer d = Integer.valueOf(128);
        System.out.println("128 == 128:      " + (c == d));
        System.out.println("128 equals 128:  " + c.equals(d));
    }
}

How It Works

  1. Small numbers are shared. valueOf keeps one object for each value from -128 to 127, so both 127 variables point to the same object and == is true.
  2. Larger numbers are not. 128 is outside the cache, so each call builds a fresh object and == compares two different references.
  3. equals is always right. It compares the numbers inside, so it prints true on both sides of the limit.
  4. The lesson. Never use == to compare two Integer objects; use equals() or unbox one side.

Example 3 — Meet NumberFormatException and the char Trap

See what valueOf does with a bad String and with a char.

Java
public class IntegerValueOfTraps {
    public static void main(String[] args) {
        try {
            Integer.valueOf("12 ");
        } catch (NumberFormatException e) {
            System.out.println("\"12 \" -> NumberFormatException");
        }

        Integer fromChar = Integer.valueOf('A');
        Integer fromString = Integer.valueOf("7");
        System.out.println("valueOf('A') = " + fromChar);
        System.out.println("valueOf(\"7\") = " + fromString);
        System.out.println("digit '7' as int: " + Integer.valueOf(String.valueOf('7')));
    }
}

How It Works

  1. Trailing space fails. The text "12 " is not a clean number, so the String form throws NumberFormatException.
  2. A char is widened. 'A' is a char, so Java picks valueOf(int) and uses its character code, 65.
  3. A String is parsed. "7" goes to the String form and becomes 7.
  4. 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.

Back to the method index

See all 37 Integer method guides grouped by what they do.

Method index →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

7 people found this page helpful