Programming
Data Type Conversion
Java
Coding Tips
String Manipulation

How do I convert from int to String?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

In Java, converting an int to a String is simple, but the best method depends on the situation. Most of the time, String.valueOf or Integer.toString is the right answer. The interesting part is not whether conversion is possible, but which API communicates intent clearly and avoids surprises when the value is not a primitive.

The usual answer: String.valueOf

For general application code, String.valueOf is the safest default. It is short, explicit, and works naturally when you are converting a primitive int.

java
1public class IntToStringExample {
2    public static void main(String[] args) {
3        int count = 42;
4        String text = String.valueOf(count);
5
6        System.out.println(text);
7    }
8}

This is the form many teams standardize on because it reads well and works consistently in ordinary code.

Integer.toString is equally valid

If you want to emphasize that the conversion is specifically about an integer, Integer.toString is also a good option.

java
1public class IntegerToStringExample {
2    public static void main(String[] args) {
3        int count = 42;
4        String text = Integer.toString(count);
5
6        System.out.println(text);
7    }
8}

For primitive int values, there is no practical difference in normal business code. This is mostly a style choice.

String concatenation works, but it is not the clearest tool

Java will convert an integer to text automatically if you concatenate it with a string.

java
1public class ConcatenationExample {
2    public static void main(String[] args) {
3        int count = 42;
4        String text = "" + count;
5
6        System.out.println(text);
7    }
8}

This works, but it is often less clear than calling a conversion method directly. If the whole goal is conversion, String.valueOf(count) communicates intent better than relying on concatenation side effects.

Formatting is the right choice when presentation matters

Sometimes you are not merely converting a value to text. You are formatting it for display, logging, or export. In those cases, use formatting APIs instead of a plain conversion.

java
1public class FormattingExample {
2    public static void main(String[] args) {
3        int invoiceNumber = 27;
4
5        String plain = String.valueOf(invoiceNumber);
6        String padded = String.format("%05d", invoiceNumber);
7
8        System.out.println(plain);
9        System.out.println(padded);
10    }
11}

Here the second string is not just "27". It is "00027", which is a presentation rule, not a basic type conversion.

Wrapper types add one important nuance

The most useful edge case to understand is the difference between a primitive int and an Integer object reference. If you have an Integer that might be null, toString() can throw a NullPointerException, while String.valueOf handles null by returning the string "null".

java
1public class WrapperExample {
2    public static void main(String[] args) {
3        Integer value = null;
4
5        String safe = String.valueOf(value);
6        System.out.println(safe);
7
8        // value.toString();  // would throw NullPointerException
9    }
10}

This distinction matters most in code that receives boxed values from collections, frameworks, or optional database fields.

Common Pitfalls

The most common mistake is using string concatenation when the code is really doing a plain conversion. It works, but it hides the intent.

Another issue is forgetting the difference between int and Integer. A primitive cannot be null, but a boxed Integer can, and calling toString() on it will fail.

Developers also use simple conversion when they really need formatting. If the output must include padding, grouping, or locale-aware rendering, use formatting APIs instead of String.valueOf.

Finally, avoid overthinking micro-differences between String.valueOf and Integer.toString in normal code. Readability and consistency matter more.

Summary

  • In Java, String.valueOf(int) is the safest general-purpose way to convert an int to a String.
  • 'Integer.toString(int) is equally valid and often used for clarity around integer conversion.'
  • String concatenation works, but it is usually less explicit than a real conversion call.
  • Use String.format when you need formatted output rather than plain conversion.
  • Be careful with Integer wrappers, because toString() on null throws an exception.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.