4 Useful Methods to Convert Java Int to String with Examples

In this guide, we will show you the easiest and most useful ways to convert an int to a String – along with code examples and best practices.

java int to string

Converting a primitive int (e.g. int number = 10;) to a String is a very basic and frequently occurring task in Java programming. There are times when you may need to display a number to the user, add it to a log, or send it in an API request – in such cases this conversion becomes necessary.

The task may seem small, but there are many ways to do it in Java. Which way is best – depends on whether you want to make the code readable, need performance, or need formatting.

When to Convert Integer to String?

There are many times when we need to convert an integer to a text in coding. Below are some common situations where this conversion is necessary:

  • Text Concatenation: When a number needs to be combined with text. For example, if you want to create a line like “Your score is: 100”, then 100 must first be converted to a string.
  • Showing in a User Interface (UI): If you are creating a GUI (such as in Swing or JavaFX), then elements like JLabel or Text Field need text to give data – numbers cannot be given directly.
  • File I/O: When you want to save a number in a text file, it has to be converted to a string first. Otherwise, it will not be saved in the file correctly.
  • Data Format of API: Many APIs (especially those related to web services or databases) take data in string form only. In such a situation, it becomes necessary to convert the number first.
Academy Pro

Java Programming Course

Learn Java the right way! Our course teaches you essential programming skills, from coding basics to complex projects, setting you up for success in the tech industry.

16.05 Hrs
3 Projects
Learn Java Programming

Method 1: Using String.valueOf(int)

This method is the most straightforward, easy and commonly recommended method when you need to convert an int to a String.

How does String.valueOf() work?

String.valueOf() is a static method in the String class and is designed for this purpose. This method takes an integer and returns its string form.

Example:

public class IntToStringExample {
    public static void main(String[] args) {
        int score = 95;
        // Converting an int to a String using String.valueOf()
        String scoreAsString = String.valueOf(score);
        System.out.println("The integer value is: " + score);
        System.out.println("The string value is: " + scoreAsString);
        // To show that this is indeed a String, we can find its length
        System.out.println("Length of the string is: " + scoreAsString.length());
    }
}

Output:

The integer value is: 95
The string value is: 95
Length of the string is: 2

Why is String.valueOf() considered a good option?

  • Clarity: When we use String.valueOf(), the person reading the code immediately understands that we want to convert a value to a String. This means there is no scope for confusion.
  • Reliability: This is a standard and built-in method of Java’s library, which is meant for just this purpose, converting something to a string. This means there is no need to manually apply tricks.
  • Null-Safe: This point is a bit interesting. If there is a primitive type (like int), then the question of null does not arise. But if there is an Object wrapper (like Integer) and null is entered in it, then String.valueOf(null) simply returns a “null” string – it does not give any error or NullPointerException.

Method 2: Using Integer.toString(int)

This method is also very good and efficient. This is a static method that comes in Java’s Integer wrapper class.

How does Integer.toString() work?

When we want to convert a primitive int (meaning a simple number like int quantity = 150) to a string, then this method works exactly like String.valueOf(int).

Example Code:

public class IntToStringExample {
    public static void main(String[] args) {
        int quantity = 150;
        // Converting int to string using Integer.toString()
        String quantityAsString = Integer.toString(quantity);
        System.out.println("The integer value is: " + quantity);
        System.out.println("The string value is: " + quantityAsString);
    }
}

Output:

The integer value is: 150
The string value is: 150

Short Summary:

If you want to convert int to String, then Integer.toString() is a safe, simple and fast method. Java developers also use it a lot.

What is the difference between String.valueOf() and Integer.toString()?

If you have a primitive int (meaning a normal number, not an object), then there is almost no difference between the two methods.

Actually, when you write String.valueOf(int i), this method automatically calls Integer.toString(i) from inside. That means the work and performance of both is exactly the same.

So what is the difference?

Just the coding style. Whether you write String.valueOf(123) or Integer.toString(123), both will give the result “123”.

Method 3: Using String Concatenation

This is a quick and easy method in which we use Java’s string concatenation feature.

How does this method work?

When you concatenate a String with another data type (such as int) using the + operator, Java automatically converts the data to a String.

Example:

public class IntToStringExample {
    public static void main(String[] args) {
        int pageNumber = 24;
        // Using String concatenation
        String pageNumberAsString = "" + pageNumber;
        System.out.println("The integer value is: " + pageNumber);
        System.out.println("The string value is: " + pageNumberAsString);
    }
}

Output:

The integer value is: 24
The string value is: 24

In short:

Just add + to empty string and int or any number will be converted to String. Shortcut method but does the job for sure.

Is conversion via string concatenation the right way to go?

  • For simple cases – yes, it works: If you’re just looking for quick things like logging a value or debugging, the "" + number method is pretty short and easy. It’s done in one line, so people often use it.
  • Performance – not so good: This method is a bit heavy in the background. When you do "" + number, the Java compiler creates a StringBuilder object internally, then appends an empty string, then converts the number to a string and appends it.

    All of this adds up to a lot of steps, compared to just using Integer.toString(number) directly, which takes fewer steps.
  • When to avoid: If you’re working in performance-critical code (such as inside loops), "" + number should be avoided. Otherwise, it can be slow.

Method 4: Using String.format()

This method is the most powerful and flexible, but it is a bit heavy if you only need to perform simple conversions. Its real strength is in creating formatted strings.

How to convert int to string using String.format()?

It uses a placeholder named %d, which stands for an integer value.

Example:

public class IntToStringExample {
    public static void main(String[] args) {
        int userId = 404;
        // Use of String.format()
        String userIdAsString = String.format("%d", userId);
        System.out.println("The string value is: " + userIdAsString);
    }
}

Output:

The string value is: 404

When to use String.format()?

This method is very useful when the goal is not just to convert a number to a string, but to do extra formatting on it, such as:

  • Zero-padding (e.g. 001, 002)
  • Proper alignment (bringing text in equal lines)
  • Inserting a number in a sentence in a stylish way.

A quick guide: When to use which method

MethodWhen is it correctCode readabilityPerformance
String.valueOf(i)General use, when you need clean and clear codeExcellentExcellent
Integer.toString(i)Excellent for fast and efficient codeExcellentExcellent
"" + iWhen you need to create a string quicklyFair enoughNot the best
String.format("%d", i)When you need to do complex formatting (like padding etc.)Fair enoughSlightly slow (due to extra overhead)

Our advice: If you are coding on a daily basis, then use String.valueOf(i) only. This is a simple, clean and easily understood method everywhere.

Frequently Asked Questions (FAQs)

Q: What is the difference between int and Integer?

A: int is a primitive data type – meaning it can store only a simple number (e.g. 5, 100). Integer is a wrapper class – it converts a primitive int into an object, allowing you to use it in collections (e.g. ArrayList) and run methods on it.

Q: How to convert a String back to an int?

A: The Integer.parseInt() method is used for this.

Example:

String s = "123";
int i = Integer.parseInt(s);

Q: What is the fastest way to convert an int to a String?

A: Both Integer.toString() and String.valueOf() are considered the fastest. But honestly, the performance difference is so small that it won’t matter in 99% of projects. So readability is more important. Don’t worry about performance optimization unless it is absolutely necessary.

Avatar photo
Great Learning Editorial Team
The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.
Scroll to Top