Java
Programming
Coding
String Conversion
Integer Objects

Java int to String - Integer.toString(i) vs new Integer(i).toString()

Master System Design with Codemia

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

Introduction

If you want to convert an int to a String in Java, Integer.toString(i) is the direct and preferred approach. new Integer(i).toString() produces the same text, but it creates an unnecessary wrapper object first. That extra allocation buys you nothing and uses an old construction style that modern Java code should avoid.

What the Two Expressions Do

These expressions both end up producing the same string content:

java
int i = 123;
String a = Integer.toString(i);
String b = new Integer(i).toString();

The difference is in the path taken.

Integer.toString(i) is a static method that converts the primitive value directly.

new Integer(i).toString() does this instead:

  1. allocate an Integer object
  2. store the primitive inside that object
  3. call the instance toString() method

That extra object is unnecessary when your goal is only text conversion.

Prefer the Direct Static Method

A minimal example:

java
1public class Demo {
2    public static void main(String[] args) {
3        int i = 123;
4        String s = Integer.toString(i);
5        System.out.println(s);
6    }
7}

This is clearer, cheaper, and idiomatic.

In real code, String.valueOf(i) is also common and equally reasonable when the context is generic value-to-string conversion.

java
int i = 123;
String s = String.valueOf(i);

Why new Integer(i) Is a Bad Habit

Constructing wrapper objects manually is old Java style. It creates unnecessary heap objects and adds noise to the code.

Even if the JVM can optimize some cases, the source code is still communicating the wrong intent. You are not trying to model an Integer object. You are trying to convert a primitive to text.

That is why the direct static conversion is the better expression of the real operation.

What About Integer.valueOf(i)?

If you actually need an Integer object, use Integer.valueOf(i) instead of new Integer(i).

java
Integer boxed = Integer.valueOf(123);
System.out.println(boxed.toString());

Integer.valueOf can reuse cached instances for common values, which is better than forced object construction. But if you only need a string, Integer.toString or String.valueOf is still the simpler answer.

Performance Difference

For one conversion in ordinary application code, the performance difference is usually small in absolute terms. But the direct static method is still better because it avoids needless allocation and communicates intent clearly.

In tight loops or heavily used formatting code, avoiding unnecessary wrapper creation matters more.

So the rule is not just micro-optimization. It is clean code plus avoiding pointless work.

Readability Matters Too

This comparison is not only about speed.

  • 'Integer.toString(i) says exactly what is happening'
  • 'new Integer(i).toString() makes the reader wonder why an object is being created'

Good Java code tends to prefer the expression that is both cheaper and clearer. In this case, those two goals point to the same answer.

Common Pitfalls

The biggest mistake is thinking object-oriented style means wrapping primitives unnecessarily before every operation.

Another mistake is using new Integer(i) in modern code when the code does not actually need a wrapper instance.

A third issue is focusing only on performance and missing the readability point: the direct conversion method better matches the intention.

Summary

  • 'Integer.toString(i) is the preferred direct way to convert an int to a String'
  • 'new Integer(i).toString() creates an unnecessary wrapper object first'
  • If you truly need an Integer object, prefer Integer.valueOf(i) over manual construction
  • 'String.valueOf(i) is also a good modern choice for generic value-to-string conversion'
  • The best option is the one that avoids needless allocation and expresses intent clearly

Course illustration
Course illustration

All Rights Reserved.