ZetCode

Java int to String conversion

Last modified October 13, 2026 Java 25

In this article we show how to convert integers to strings in Java. We cover the built-in conversion methods Integer.toString and String.valueOf, string concatenation, string formatting, and StringBuilder. We also show how to convert numbers to other bases, format them with padding and digit grouping, handle Integer objects that may be null, and convert whole collections of integers.

Converting an int to a String produces the textual representation of the number; for example, the value 16 becomes the two-character string "16". This is not a cast: an int is a primitive type and a String is an object, so an expression such as (String) 16 does not compile. The conversion is always done by a method, either called explicitly or invoked behind the scenes.

Using Integer.toString

The Integer.toString(int) static method returns the signed decimal representation of its argument as a string. It is the most direct way to convert an int to a String.

Main.java
void main() {

    int apples = 16;
    int temperature = -7;

    String s1 = Integer.toString(apples);
    String s2 = Integer.toString(temperature);

    System.out.println(s1);
    System.out.println(s2);
    System.out.println(s1 + s2);
}

We convert two integers to strings. Negative numbers get a leading minus sign.

System.out.println(s1 + s2);

Since both values are now strings, the + operator joins them instead of adding them.

$ java Main.java
16
-7
16-7

Using String.valueOf

The String.valueOf(int) method returns the string representation of the integer argument. In the JDK, it simply delegates to Integer.toString, so the two methods give identical results. Many developers prefer String.valueOf because the same method name works for all primitive types and for objects.

Main.java
void main() {

    int apples = 16;

    String s = String.valueOf(apples);
    String msg = "There are " + s + " apples";

    System.out.println(msg);
}

The example converts the integer with String.valueOf and uses the result to build a message.

$ java Main.java
There are 16 apples

Using string concatenation

When one operand of the + operator is a String, the other operand is converted to a string automatically. This is the shortest way to embed a number in text. Concatenating with an empty string, "" + n, is a common shorthand for the conversion itself, although String.valueOf(n) states the intent more clearly.

Main.java
void main() {

    int apples = 16;

    String msg = "There are " + apples + " apples";
    String s = "" + apples;

    System.out.println(msg);
    System.out.println(s.length());
}

The example uses string concatenation to build a message and to convert an integer to a string. The length of the string "16" is 2.

Since Java 9, the compiler translates string concatenation into an invokedynamic call handled by StringConcatFactory, which generates optimized code at runtime. Older Java versions used a chain of StringBuilder.append calls.

Keep in mind that + is evaluated from left to right. The expression 1 + 2 + " apples" gives "3 apples", while "apples: " + 1 + 2 gives "apples: 12".

$ java Main.java
There are 16 apples
2

Using String.format and formatted

The String.format method returns a string built from a format string and arguments. The %d specifier formats an integer as a decimal number. Since Java 15, the formatted instance method provides the same functionality, called directly on the format string.

Main.java
void main() {

    int apples = 16;

    String msg1 = String.format("There are %d apples", apples);
    String msg2 = "There are %d apples".formatted(apples);

    System.out.println(msg1);
    System.out.println(msg2);
}

Both lines produce the same string. Formatting is more verbose than the other approaches, but it shines when the output needs padding, signs, or digit grouping, as shown later.

$ java Main.java
There are 16 apples
There are 16 apples

Using StringBuilder

StringBuilder represents a mutable sequence of characters. Its append(int) method adds the decimal representation of an integer. StringBuilder is useful when a string is built in many steps, for instance in a loop.

Main.java
void main() {

    int apples = 16;

    var builder = new StringBuilder();
    builder.append("There are ").append(apples).append(" apples");

    String msg = builder.toString();
    System.out.println(msg);
}

The append calls can be chained, because each returns the builder. The final string is obtained with toString.

$ java Main.java
There are 16 apples

Converting Integer objects

An Integer wrapper object has its own toString method. Unlike a primitive int, an Integer reference can be null, and calling toString on it then throws a NullPointerException. The String.valueOf(Object) and Objects.toString methods handle null safely.

Main.java
void main() {

    Integer apples = 16;
    Integer missing = null;

    System.out.println(apples.toString());
    System.out.println(String.valueOf(missing));
    System.out.println(Objects.toString(missing, "unknown"));

    // missing.toString() would throw NullPointerException
}

String.valueOf returns the string "null" for a null reference. Objects.toString lets us supply a default value instead.

$ java Main.java
16
null
unknown

Converting to other bases

The Integer.toString(int, int) overload takes a radix as its second argument, from 2 to 36. The Integer class also has the toBinaryString, toOctalString, and toHexString convenience methods.

Main.java
void main() {

    int n = 255;

    System.out.println(Integer.toString(n, 2));
    System.out.println(Integer.toString(n, 8));
    System.out.println(Integer.toString(n, 16));
    System.out.println(Integer.toString(n, 36));

    System.out.println(Integer.toBinaryString(n));
    System.out.println(Integer.toOctalString(n));
    System.out.println(Integer.toHexString(n));

    System.out.println(Integer.toString(-n, 16));
    System.out.println(Integer.toHexString(-n));
}

The value 255 is converted to binary, octal, hexadecimal, and base-36 strings.

System.out.println(Integer.toString(-n, 16));
System.out.println(Integer.toHexString(-n));

The two approaches differ for negative numbers. Integer.toString prefixes a minus sign to the magnitude, while toHexString treats the value as unsigned and shows its two's complement bit pattern.

$ java Main.java
11111111
377
ff
73
11111111
377
ff
-ff
ffffff01

Formatting integers

Format specifiers control how a number appears in the resulting string. The , flag adds grouping separators, + always shows the sign, 0 pads with zeros, a width sets the minimum field size, and - aligns to the left. For locale-specific output, NumberFormat can be used.

Main.java
import java.text.NumberFormat;

void main() {

    int n = 1234567;

    System.out.println(String.format("%,d", n));
    System.out.println(String.format("%+d", n));
    System.out.println(String.format("%010d", n));
    System.out.println(String.format("[%10d]", n));
    System.out.println(String.format("[%-10d]", n));

    var nf = NumberFormat.getInstance(Locale.GERMANY);
    System.out.println(nf.format(n));
}

The String.format method uses the default locale, so the grouping separator in the first line depends on the system settings. The NumberFormat instance for Locale.GERMANY uses a dot.

$ java Main.java
1,234,567
+1234567
0001234567
[   1234567]
[1234567   ]
1.234.567

Converting a list of integers

With streams, we can convert a whole collection of integers to strings by mapping each element with String::valueOf.

Main.java
import java.util.stream.Collectors;

void main() {

    List<Integer> vals = List.of(1, 2, 3, 4, 5);

    List<String> strs = vals.stream()
            .map(String::valueOf)
            .toList();
    System.out.println(strs);

    String joined = vals.stream()
            .map(String::valueOf)
            .collect(Collectors.joining(", "));
    System.out.println(joined);
}

The first stream produces a list of strings. The second joins the converted values into a single string with Collectors.joining.

$ java Main.java
[1, 2, 3, 4, 5]
1, 2, 3, 4, 5

Which method to use

The reverse conversion, from a string to an integer, is done with Integer.parseInt or Integer.valueOf.

Source

Java Integer - language reference

Java String - language reference

In this article we have shown how to convert integers to strings in Java.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Java tutorials.