Java int to String conversion

Java int to String tutorial shows how to convert integers to strings. There are several ways to perform int to String conversion in Java. We can use string concatenation, string formatting, string building, and use built-in conversion methods.

Integer to String conversion

Integer to String conversion is a type conversion or type casting, where an entity of integer data type is changed into string one.

In the examples of this tutorial, we build a string message that contains an integer.

Java int to String with String.format()

String.format() returns a formatted string using the specified format string and arguments.

JavaIntToString.java
package com.zetcode;

public class JavaIntToString {

    public static void main(String[] args) {
        
        int numOfApples = 16;
        
        String msg = String.format("There are %s apples", numOfApples);
        
        System.out.println(msg);             
    }
}

The example uses String.format() to do int to String conversion.

Java int to String with string concatenation

When we use the + operator on int and String parameters, the Java compiler internally performs type conversion. Note that this conversion is disliked by many Java programmers.

JavaIntToString2.java
package com.zetcode;

public class JavaIntToString2 {

    public static void main(String[] args) {
        
        int numOfApples = 16;
        
        String msg = "There are " + numOfApples + " apples";
        
        System.out.println(msg);   
    }
}

The example uses string concatenation to do int to String conversion. Internally, Java compiler uses StringBuilder to do the conversion.

Java int to String with Integer.toString()

Integer.toString() converts its argument to signed decimal representation and returned as a string.

JavaIntToString3.java
package com.zetcode;

public class JavaIntToString3 {

    public static void main(String[] args) {
        
        int numOfApples = 16;
        
        String msg = "There are " + Integer.toString(numOfApples) + " apples";
        
        System.out.println(msg);          
    }
}

The example uses Integer.toString() to do int to String conversion.

Java int to String with String.valueOf()

String.valueOf() returns the string representation of the integer argument. Java compiler internally calls Integer.toString() when String.valueOf() is used.

String.valueOf() is considered the most readable approach by many.

JavaIntToString4.java
package com.zetcode;

public class JavaIntToString4 {

    public static void main(String[] args) {

        int nOfApples = 16;
        
        String msg = "There are " + String.valueOf(nOfApples) + " apples";
        
        System.out.println(msg);              
    }
}

The example uses String.valueOf() to do int to String conversion.

Java int to String with StringBuilder

StringBuilder represents a mutable string of characters. We can use StringBuilder to construct strings. We can append integers to the builder as well.

JavaIntToString5.java
package com.zetcode;

public class JavaIntToString5 {

    public static void main(String[] args) {

        int numOfApples = 16;
        
        StringBuilder msg = new StringBuilder();
        msg.append("There are ").append(numOfApples).append(" apples");
        
        System.out.println(msg);          
    }
}

The code example uses StringBuilder to do int to String conversion.

Java int to String example

The following example reads an integer from the user with Scanner. Then a string message is printed to the console, containing the user's input.

JavaIntToStringEx.java
package com.zetcode;

import java.util.Scanner;

public class JavaIntToStringEx {

    public static void main(String[] args) {

        System.out.print("Enter number of apples: ");

        try (Scanner scan = new Scanner(System.in)) {
            
            int numOfApples = scan.nextInt();
            
            String msg = String.format("There are %d apples", numOfApples);
            
            System.out.println(msg);
        }
    }
}

We use String.format() to do int to String conversion.

try (Scanner scan = new Scanner(System.in)) {

Scanner is a simple text scanner which can parse primitive types and strings using regular expressions. We use Scanner to read user's input.

int numOfApples = scan.nextInt();

The nextInt() method scans the next token of the input as an int.

String msg = String.format("There are %d apples", numOfApples);

A message is created with String.format(). It takes the user's input as the second parameter.

In this tutorial, we have shown how to convert integers to strings in Java. You might also be interested in the related tutorials: Java StringBuilder tutorial, Java HashSet tutorial, Java HashMap tutorial, Java static keyword, HashMap iteration in Java, Java 8 forEach tutorial, Reading text files in Java, Reading and writing ICO images in Java, Java tutorial, Displaying image in Java.