Integer.parseInt() vs Integer.valueOf()
Fast recall
Quick revision
Core answer
Integer.parseInt(text) parses and returns a primitive int. Integer.valueOf(text) parses and returns an Integer, using valueOf(int) and therefore the required -128 to 127 cache when applicable. Choose from the needed return type. Both throw NumberFormatException for invalid or out-of-range decimal text unless a different radix overload is used.Whether you know the return type difference and can choose the right one for a given context.
Using valueOf() everywhere thinking it's more 'OOP'. For pure numeric computation where you don't need a collection, parseInt() is lighter — no object, no boxing.
Mention that Integer.valueOf(int) is what autoboxing calls — Integer x = 5 is exactly Integer.valueOf(5). And that Integer.valueOf(String) actually calls Integer.parseInt() internally and then boxes the result.
Interview-ready guide
Interview answer
It really comes down to the type you get back and what you'll do with it. parseInt() hands you a primitive int — no object, nothing on the heap — so it's the right choice when you're parsing a number to compute with or assign to an int. valueOf() hands you an Integer object, which is what you want when the number has to go into something like an ArrayList.
I'd gently correct the common habit of reaching for valueOf() everywhere because it feels more object-oriented. For plain numeric work that just allocates an object you don't need — parseInt() is lighter, and autoboxing will box the value for you anyway when a collection actually needs it.
A couple of details that show real understanding. valueOf(int) is exactly what autoboxing calls — Integer x = 5 compiles to Integer.valueOf(5) — and it uses the cache for -128 to 127, which is why == between two small Integers sometimes appears to work. And valueOf(String) actually calls parseInt() internally and then boxes the result, so it does strictly more work. Both throw NumberFormatException on bad input, so either way you wrap user input in a try-catch. My default is `parseInt()` for computation, and I let autoboxing handle the boxing when it's genuinely needed.
Integer.parseInt() vs Integer.valueOf()
Direct Answer
Integer.parseInt(text) parses and returns a primitive int. Integer.valueOf(text) parses and returns an Integer, using valueOf(int) and therefore the required -128 to 127 cache when applicable. Choose from the needed return type. Both throw NumberFormatException for invalid or out-of-range decimal text unless a different radix overload is used.
Code Example
import java.util.ArrayList;
public class ParseVsValueOf {
public static void main(String[] args) {
// parseInt — returns primitive int, no object
int a = Integer.parseInt("42"); // primitive 42
int b = Integer.parseInt("100");
System.out.println(a + b); // 142 — direct arithmetic, no boxing
// valueOf — returns Integer object
Integer x = Integer.valueOf("42"); // Integer object
ArrayList<Integer> list = new ArrayList<>();
list.add(Integer.valueOf("99")); // object added to list
// or just: list.add(Integer.parseInt("99")); — autoboxing does valueOf() for you
// Both throw NumberFormatException for invalid input
try {
int bad = Integer.parseInt("abc"); // NumberFormatException
} catch (NumberFormatException e) {
System.out.println("Not a number: " + e.getMessage());
}
// Autoboxing calls valueOf — these are equivalent:
Integer auto = 5; // Integer.valueOf(5)
Integer manual = Integer.valueOf(5); // same thing
System.out.println(auto == manual); // true — both hit the cache
}
}
Common Mistakes & Pitfalls
- Using
valueOf()everywhere because it feels more 'OOP' — for pure numeric work it just allocates an object you don't need;parseInt()is lighter, and autoboxing will box the value anyway when a collection truly needs it. - Forgetting both throw
NumberFormatException—parseInt("abc")andvalueOf("abc")crash identically; never parse user input without a try-catch. - Comparing
valueOf()results with==— it works for-128to127by cache luck, then silently fails for larger values like1000; use.equals()or unbox toint. - Confusing
valueOf(int)withvalueOf(String)— theintoverload is what autoboxing calls and never parses; only theStringoverload parses text. - Treating
parseInt()as 'old' andvalueOf()as 'new' — they aren't versions of each other; they return different types for different needs.
Check your understanding
Answer these without looking back at the article.
- What is the Integer cache and how does valueOf() use it?
- What exception does parseInt() throw and when?
- Is there a difference between Integer.parseInt() and new Integer(str)?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.