Sub topic 5
Integer.valueOf and Why the Constructors Are Deprecated
Prerequisites and purpose
This chapter assumes that you know Java primitive types, classes, objects, ==, equals, and basic generics. It also assumes that you have seen autoboxing, where Java converts a primitive such as int into its wrapper object, Integer.
The Integer class offers two main ways to create an Integer from a number:
Integer.valueOf(...), which is the preferred factory method.new Integer(...), whose constructors are deprecated.
A factory method is a method that creates and returns an object. The method can reuse existing objects, choose a specialized implementation, or apply other creation rules. A constructor always creates a new object.
`valueOf` for primitives and strings
Use Integer.valueOf(int) when you need an Integer object from an int. Use Integer.valueOf(String) when you need to parse a decimal string into an Integer.
public class ValueOfDemo {
public static void main(String[] args) {
int primitive = 42;
Integer fromInt = Integer.valueOf(primitive);
Integer fromText = Integer.valueOf("42");
System.out.println(fromInt); // 42
System.out.println(fromText); // 42
System.out.println(fromInt.equals(fromText)); // true
int backToPrimitive = fromText.intValue();
System.out.println(backToPrimitive); // 42
}
}The string form accepts a signed decimal number, such as "42" or "-7". It does not accept arbitrary text. Invalid input throws NumberFormatException.
public class InvalidIntegerText {
public static void main(String[] args) {
Integer number = Integer.valueOf("42");
System.out.println(number);
Integer invalid = Integer.valueOf("4.2"); // NumberFormatException
System.out.println(invalid);
}
}If you want an int rather than an Integer, use Integer.parseInt:
int count = Integer.parseInt("42");parseInt returns a primitive. valueOf returns a wrapper object. Java can also convert the result of valueOf back to int through unboxing, but choosing the method that expresses your intended result makes the code clearer.
| Method | Input | Result | Typical use |
|---|---|---|---|
| `Integer.parseInt("42")` | `String` | `int` | Arithmetic or primitive variables |
| `Integer.valueOf(42)` | `int` | `Integer` | Collections or APIs requiring objects |
| `Integer.valueOf("42")` | `String` | `Integer` | Parsing when an object is needed |
| `number.intValue()` | `Integer` | `int` | Explicit unboxing |
Why `new Integer(...)` is deprecated
Older Java code sometimes uses these constructors:
Integer a = new Integer(42);
Integer b = new Integer("42");The constructors were deprecated in Java 9 because they force a new object to be allocated. The valueOf methods can reuse cached wrapper objects and communicate the intended factory behavior.
The constructors also encourage a mistaken assumption: that every numeric wrapper needs a distinct object. Usually, it does not. An Integer represents a value, and shared immutable objects are safe because an Integer cannot be changed after creation.
public class CreationChoices {
public static void main(String[] args) {
Integer preferred = Integer.valueOf(42);
Integer alsoPreferred = 42; // autoboxing uses Integer.valueOf(42)
System.out.println(preferred.equals(alsoPreferred)); // true
}
}Do not replace deprecated constructors with new in new code. Replace them with either parseInt or valueOf, depending on whether the receiving code needs a primitive or a wrapper.
What autoboxing chooses
When Java needs an Integer and receives an int, the compiler inserts a call similar to Integer.valueOf:
Integer number = 10;
// Similar to:
Integer numberExplicit = Integer.valueOf(10);This relationship explains why valueOf is the normal creation path for boxed integers.
Exercise
Exercise
Which method should you use for each result: a primitive int from "900", or an Integer object from "900"? Why should you avoid new Integer(900)?
Show answer
Use Integer.parseInt("900") for the primitive and Integer.valueOf("900") for the object. Avoid the constructor because it is deprecated and always requests a new wrapper object instead of allowing the factory to reuse a cached object.