Java
Programming
Coding Methods
parseInt()
valueOf()

Difference between parseInt() and valueOf() in Java?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Integer.parseInt() and Integer.valueOf() both turn text into an integer value, so they often look interchangeable. The main difference is the return type: parseInt returns a primitive int, while valueOf returns an Integer object.

Return Type Is the Core Difference

If you need a primitive int, use parseInt:

java
int count = Integer.parseInt("42");
System.out.println(count + 1);

If you need an Integer object, use valueOf:

java
Integer count = Integer.valueOf("42");
System.out.println(count + 1);

In many places the difference seems small because Java can autobox and unbox automatically. For example, an Integer can often be used where an int is expected, and vice versa. But the distinction still matters for APIs, collections, nullability, and object identity.

Parsing Behavior Is Very Similar

Both methods parse the string content the same way and both throw NumberFormatException if the string is not a valid integer representation.

java
1try {
2    int x = Integer.parseInt("12x");
3    System.out.println(x);
4} catch (NumberFormatException ex) {
5    System.out.println("parseInt failed");
6}
7
8try {
9    Integer y = Integer.valueOf("12x");
10    System.out.println(y);
11} catch (NumberFormatException ex) {
12    System.out.println("valueOf failed");
13}

They also both support a radix overload:

java
1int binary = Integer.parseInt("1010", 2);
2Integer hex = Integer.valueOf("FF", 16);
3
4System.out.println(binary);
5System.out.println(hex);

So the real question is usually not "which one parses better" but "do I want a primitive or an object."

Why valueOf Can Matter

Because valueOf returns an object, it fits naturally with generic collections and APIs that require wrapper types:

java
1import java.util.ArrayList;
2import java.util.List;
3
4public class Example {
5    public static void main(String[] args) {
6        List<Integer> values = new ArrayList<>();
7        values.add(Integer.valueOf("100"));
8        System.out.println(values);
9    }
10}

You could still call parseInt("100") here and let autoboxing wrap the primitive automatically, but using valueOf makes the object-returning intent explicit.

valueOf also participates in integer caching for a range of small values, so repeated calls may reuse existing Integer objects instead of always constructing new ones. That is a JVM-level optimization detail rather than the main reason to choose the method, but it explains why valueOf exists as a distinct API instead of forcing callers to box manually every time.

Choosing the Right One

A simple rule works well:

  • use parseInt when you want a primitive for arithmetic or control flow
  • use valueOf when you specifically need an Integer object

Examples of preferring parseInt:

  • loop bounds
  • counters
  • arithmetic expressions
  • performance-sensitive code that operates on primitives

Examples of preferring valueOf:

  • storing the result in List<Integer>
  • passing an Integer to an API that expects the wrapper type
  • working with code that needs object semantics rather than primitives

Null and Autoboxing Considerations

Neither method treats invalid input gracefully. If the string is malformed, both throw NumberFormatException. That means validation or exception handling is still your responsibility.

Also, once Integer objects are involved, remember that they can be null. A primitive int cannot. That matters when unboxing:

java
1Integer maybeValue = null;
2
3try {
4    int x = maybeValue;
5    System.out.println(x);
6} catch (NullPointerException ex) {
7    System.out.println("autounboxing null failed");
8}

This is not a valueOf bug, but it is one reason primitive and wrapper types should not be treated as identical.

Common Pitfalls

  • Assuming parseInt and valueOf differ in parsing rules rather than mainly in return type.
  • Using Integer when a primitive int would be simpler.
  • Forgetting that Integer can be null, while int cannot.
  • Comparing Integer objects with == and expecting value comparison.
  • Catching parsing errors too late instead of validating input close to the boundary.

Summary

  • 'Integer.parseInt() returns a primitive int.'
  • 'Integer.valueOf() returns an Integer object.'
  • Both parse numeric text similarly and both throw NumberFormatException on bad input.
  • Choose parseInt for primitive-oriented logic and valueOf when an object is actually needed.
  • The difference matters most in collections, API signatures, nullability, and object behavior.

Course illustration
Course illustration

All Rights Reserved.