Converting Int to String in Java

To convert an int to a String in Java, use the String.valueOf() method or Integer.toString(). Both return the decimal digits of the number as a string, with a minus sign for a negative number. Adding the number to an empty string gives the same result. To pad the number with leading zeros, use String.format(). In this Java Int to String Example, we convert the same number to a string in three ways. Click Execute to run the Java Int to String Example online and see the result.
Converting Int to String in Java Execute
public class IntToStringDemo {
    public static void main(String[] args) {
        int number = 12345;

        String str1 = String.valueOf(number);
        System.out.println("Using String.valueOf(): " + str1);

        String str2 = Integer.toString(number);
        System.out.println("Using Integer.toString(): " + str2);

        String str3 = "" + number;
        System.out.println("Using concatenation: " + str3);
    }
}

Java Int to String Syntax

valueOf() is a static method of the String class, and toString() is a static method of the Integer class. For an int, String.valueOf() returns exactly what Integer.toString() returns.

Java Int to String Syntax
String.valueOf(int i)
Integer.toString(int i)
Integer.toString(int i, int radix)

Java Int to String Examples

The following are examples of converting an int to a string in Java. You can run any of these examples in the online Java code runner and try them with your own numbers.

Convert an int to String with String.valueOf()

valueOf() returns the digits of the number as a new String. String methods such as length() then work on the result.

Java String.valueOf() Example
public class ValueOfExample {
    public static void main(String[] args) {
        int count = 42;
        String text = String.valueOf(count);
        System.out.println(text + " items");
        System.out.println(text.length());
    }
}

// output:
// 42 items
// 2

Convert an int to String with Integer.toString()

Integer.toString() takes an optional radix. Pass 2 for a binary string or 16 for a hexadecimal one.

Java Integer.toString() Example
public class ToStringExample {
    public static void main(String[] args) {
        int number = 255;
        System.out.println(Integer.toString(number));
        System.out.println(Integer.toString(number, 2));
        System.out.println(Integer.toString(number, 16));
        System.out.println(Integer.toString(-number, 16));
    }
}

// output:
// 255
// 11111111
// ff
// -ff

Convert an int to String with concatenation

The + operator turns a number into a string when the other operand is a string. Java evaluates + from left to right, so numbers added before the first string are summed as numbers.

Java Int to String Concatenation Example
public class ConcatenationExample {
    public static void main(String[] args) {
        int a = 1;
        int b = 2;
        System.out.println("" + a + b);
        System.out.println(a + b + "");
    }
}

// output:
// 12
// 3

Convert an int to String with leading zeros

String.format() with the %0Nd format pads the number with zeros to N digits. A number that already has N digits or more is not cut.

Java String.format() Leading Zeros Example
public class LeadingZerosExample {
    public static void main(String[] args) {
        System.out.println(String.format("%05d", 42));
        System.out.println(String.format("%03d", 7));
        System.out.println(String.format("%03d", 12345));
    }
}

// output:
// 00042
// 007
// 12345

See also

Updated: