Java Integer.valueOf: Caching and Usage Explained
Understand Java's Integer.valueOf() method: its caching behavior, differences from parseInt, and when to use it for efficient and correct code.

When you call Integer.valueOf() in Java, you are not just converting a string or an int into an Integer object. The method participates in a caching mechanism that can affect both memory usage and object identity. Understanding how Integer.valueOf behaves under the hood helps you write code that is both efficient and predictable, especially when you rely on reference equality or need to minimize object allocation.
What Integer.valueOf Actually Does
The Integer class provides two overloaded valueOf methods:
public static Integer valueOf(int i)
public static Integer valueOf(String s) throws NumberFormatExceptionThe first accepts a primitive int and returns an Integer instance. The second parses a String and returns an Integer. Both are static factory methods, meaning they return an instance without requiring a new call. The key difference from new Integer() is that valueOf may return a cached instance rather than creating a fresh object every time.
For example:
Integer a = Integer.valueOf(42);
Integer b = Integer.valueOf(42);
System.out.println(a == b); // true, because 42 is in the cache rangeThis behavior is not just an implementation detail. For values between -128 and 127, it is required by the Java Language Specification for boxing conversions, as we will see shortly.
The Integer Cache Range
The Java Language Specification requires that boxing conversions of int values in the range -128 to 127 produce the same object. Integer.valueOf(int) follows that rule, and the default JVM cache covers those values. Some JVMs allow the upper bound to be extended with the system property java.lang.Integer.IntegerCache.high, but the default is sufficient for most applications. The cache is initialized lazily on first use and holds references to Integer objects for each value in the range.
When you call Integer.valueOf(100), you get a reference to the cached object. When you call it again with the same value, you get the same reference. This is why == comparison works for these values but fails for values outside the range:
Integer c = Integer.valueOf(200);
Integer d = Integer.valueOf(200);
System.out.println(c == d); // false, because 200 is outside the default cache rangeIf you need to compare Integer objects for value equality, always use .equals() rather than ==, regardless of the cache. The cache only guarantees identity for a limited set of values; it does not change the fundamental rule that reference equality is not value equality.
valueOf vs parseInt: When to Use Which
Integer.parseInt(String) returns a primitive int, not an Integer object. This distinction matters both for performance and for how you use the result. If you need a primitive, parseInt is the direct choice. If you need an Integer to store in a collection, use valueOf.
int primitive = Integer.parseInt("123");
Integer wrapper = Integer.valueOf("123");Using valueOf with a string parses the string and boxes the result, so it is slightly less efficient if you only need the primitive. Conversely, if you need an Integer, using parseInt and then manually boxing with Integer.valueOf is redundant. Choose the method that matches your target type to avoid unnecessary conversion.
Autoboxing and valueOf
Java's autoboxing feature relies on valueOf under the hood. When you write:
Integer num = 42;The compiler translates this to Integer.valueOf(42). This means that autoboxing benefits from the same cache. In practice, variables assigned through autoboxing within the cache range share the same object references. This behavior is not a bug; it is designed to reduce memory churn when boxing frequently used values.
However, do not assume that autoboxing is free. Each autoboxing operation outside the cache range can allocate a new Integer object, depending on the JVM. In performance-sensitive loops, this can create unnecessary garbage. If you are repeatedly converting primitives to wrappers, consider whether you can avoid boxing altogether or use primitive collections from libraries like Eclipse Collections or Trove.
Performance and Memory Implications
The primary performance benefit of Integer.valueOf is the avoidance of object creation for values in the cache range. Since the cache is pre-populated, calls to valueOf for these values are essentially free in terms of allocation. For values outside the range, each call can create a new object, which adds pressure to the garbage collector.
Consider a scenario where you parse a large number of small integers from a file:
List<Integer> numbers = new ArrayList<>();
for (String line : lines) {
numbers.add(Integer.valueOf(line));
}If the parsed values are mostly within the cache range, the list will reference cached objects, and no additional Integer instances are created. If the values are large, each valueOf call can allocate a new object, depending on the JVM's cache configuration. In such cases, using a primitive array or a specialized collection can reduce allocation overhead.
There is no benchmark data here, but the mechanism is clear: allocation is avoided only when the cache applies. For high-throughput code that deals with many out-of-range integers, consider using int[] or a primitive collection to avoid wrapper overhead entirely.
Common Pitfalls and Edge Cases
One common mistake is relying on == for Integer comparison outside the cache range. Even if two Integer objects have the same value, == will return false if they are distinct instances. Always use .equals() or unbox with .intValue() for value comparison.
Another edge case is parsing strings that are not valid integers. Both valueOf(String) and parseInt throw NumberFormatException for malformed input. You should handle this exception or validate input before conversion, depending on your application's requirements.
Also note that Integer.valueOf does not accept null strings; it will throw NumberFormatException if you pass null. If you are converting user input, consider using Optional or a custom validation step to avoid unexpected exceptions.
Compatibility and Maintainability Considerations
The caching behavior described above is required by the Java Language Specification for boxing conversions of int values in the range -128 to 127, and Integer.valueOf follows it in standard Java runtimes. However, the upper bound can be configured via a system property in some JVM implementations. This means that the exact identity behavior for values above 127 is not portable across JVMs or configurations. If your code relies on reference equality for Integer objects, it is inherently fragile. The safest approach is to treat Integer as a value type and always use .equals() for comparison.
From a maintainability perspective, using valueOf instead of new Integer() is recommended because it makes the caching behavior explicit and avoids unnecessary object creation. The new Integer(int) constructor has been deprecated since Java 9, and modern code should use valueOf or rely on autoboxing. This aligns with the general guidance to prefer static factory methods over constructors when they are available.
When you need to convert a string to an Integer, valueOf is the idiomatic choice. For primitive int, use parseInt. Keeping these two methods distinct in your code makes the intended type clear and avoids accidental boxing overhead.
Try matching browser tools
All toolsSmall utilities that fit Java work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Java guides
All Java guidesJava Final Keyword: Usage and Behavior
How Java's final keyword works for variables, methods, and classes, with practical examples and decision guidance for using it.
Java Final Variable: Syntax, Behavior, and Use Cases
Understand what a final variable in Java means, how initialization works, and when to use it for constants, immutability, and clearer code.
Java Duration vs Period: When to Use Each
java duration vs period: Compare Java Duration and Period to handle time-based and date-based calculations correctly. Learn syntax, conversion, and pitfalls.
Instance vs Static Variables in Java: Key Differences
Understand the difference between instance and static variables in Java, including memory, lifecycle, and when to use each.
More in Java
All Java guidesThe guides filed next to this one in Java, in library order.