What’s New ?

The Top 10 favtutor Features You Might Have Overlooked

Read More
Computer Science

Convert Int to String in Java (5 Ways with Examples)

Jul 19, 2026 5 Minutes Read Why Trust Us Why you can trust this guide. Written by working engineers and reviewed by our editorial team under a strict editorial policy for accuracy, clarity and zero bias. Anubhav Agarwal By Anubhav Agarwal Anubhav Agarwal Anubhav Agarwal
I'm a research-oriented engaged in various technologies and a technical content writer. Being a coder myself, going through the documentation to provide optimized solutions as technical content is what I always look for.
Connect on LinkedIn →
Convert Int to String in Java (5 Ways with Examples)

In Java, you convert an int to a String with Integer.toString(number) or String.valueOf(number). Both return a new String containing the digits of the number.

Java also converts numbers to text through string concatenation, String.format(), and StringBuilder. This lesson shows all five ways, when each one fits, and how the same methods handle negative numbers and the Integer wrapper class.

How Do You Convert an Int to a String in Java?

Call Integer.toString() with the int as its argument, and it returns the number as a String.

public class Main {
    public static void main(String[] args) {
        // 1. An int value
        int orderCount = 583;

        // 2. Convert it to a String
        String text = Integer.toString(orderCount);
        System.out.println(text);           // Output: 583
        System.out.println(text.length());  // Output: 3
    }
}

After the conversion, text is a normal String, so String methods such as length() work on it.

Five ways to convert an int to a String in Java: Integer.toString, String.valueOf, concatenation, String.format, and StringBuilder

5 Ways to Convert an Int to a String in Java

1) The Integer.toString() Method

The Integer.toString() static method is the direct conversion method for the int type. It also handles negative numbers, keeping the minus sign as the first character.

public class Main {
    public static void main(String[] args) {
        int temperature = -14;

        String reading = Integer.toString(temperature);
        System.out.println(reading);  // Output: -14
    }
}

2) The String.valueOf() Method

The String.valueOf() method produces the same result. It is overloaded for every primitive type, so the same method name works for int, long, double, and boolean values.

public class Main {
    public static void main(String[] args) {
        int seatsLeft = 42;

        String message = String.valueOf(seatsLeft);
        System.out.println(message);  // Output: 42
    }
}

3) String Concatenation

Joining an int to a string with + converts it automatically. Concatenating with an empty string is a common shortcut, though the two methods above state the intent more clearly.

public class Main {
    public static void main(String[] args) {
        int cartItems = 7;

        String label = "Items in cart: " + cartItems;
        String shortcut = "" + cartItems;
        System.out.println(label);     // Output: Items in cart: 7
        System.out.println(shortcut);  // Output: 7
    }
}

4) The String.format() Method

The String.format() method converts the number while placing it inside a template. The %d specifier stands for a decimal integer and can add padding.

public class Main {
    public static void main(String[] args) {
        int invoiceNumber = 88;

        String formatted = String.format("Invoice #%d", invoiceNumber);
        String padded = String.format("%05d", invoiceNumber);
        System.out.println(formatted);  // Output: Invoice #88
        System.out.println(padded);     // Output: 00088
    }
}

5) The StringBuilder append() Method

When you build a longer string from many pieces, StringBuilder converts each appended int without creating intermediate String objects.

public class Main {
    public static void main(String[] args) {
        int year = 2026;
        int month = 7;

        String date = new StringBuilder().append(year).append("-0").append(month).toString();
        System.out.println(date);  // Output: 2026-07
    }
}

When to Use Each Method

MethodUse it when
Integer.toString()You convert a plain int and nothing else
String.valueOf()The value's type may change later, or the value may be a null wrapper
Concatenation with +The number goes straight into a longer message
String.format()You need padding, alignment, or a template
StringBuilderYou assemble a string from many parts in a loop

Examples of Converting an Int to a String in Java

1) Building a Username From an ID

public class Main {
    public static void main(String[] args) {
        int userId = 4917;

        String username = "user_" + Integer.toString(userId);
        System.out.println(username);  // Output: user_4917
    }
}

2) Checking the Digits of a Number

Converting to a String gives access to individual digits through charAt().

public class Main {
    public static void main(String[] args) {
        int pinCode = 560034;

        String digits = String.valueOf(pinCode);
        System.out.println(digits.length());     // Output: 6
        System.out.println(digits.charAt(0));    // Output: 5
    }
}

3) Zero-Padded Ticket Numbers

public class Main {
    public static void main(String[] args) {
        int ticket = 391;

        String display = String.format("TICKET-%06d", ticket);
        System.out.println(display);  // Output: TICKET-000391
    }
}

Learn More About Int to String Conversion

Can You Cast an Int to a String in Java?

No. A cast like (String) number does not compile, because int and String are unrelated types. Casting in Java only works between compatible types, so what other languages call casting an int to a string is done with the conversion methods above.

Casting an int to a String in Java does not compile, while Integer.toString converts it correctly

Integer to String in Java

An Integer wrapper object converts with its instance method toString(), or with String.valueOf(). String.valueOf() is the safer choice for values that might be null, because calling toString() on a null reference throws a NullPointerException.

public class Main {
    public static void main(String[] args) {
        Integer stockLevel = 250;

        System.out.println(stockLevel.toString());       // Output: 250
        System.out.println(String.valueOf(stockLevel));  // Output: 250
    }
}

Int to String in a Different Base

Integer.toString() takes an optional second argument, the radix, to produce binary, octal, or hexadecimal text.

public class Main {
    public static void main(String[] args) {
        int flags = 255;

        System.out.println(Integer.toString(flags, 2));   // Output: 11111111
        System.out.println(Integer.toString(flags, 16));  // Output: ff
    }
}

Converting a String Back to an Int

The reverse conversion uses Integer.parseInt(): Integer.parseInt("583") returns the int 583, and throws a NumberFormatException if the text is not a valid number.

Key Takeaways for Int to String in Java

  • Integer.toString() - the direct method for converting an int, with an optional radix for other bases.
  • String.valueOf() - works for every primitive type and handles null wrapper references without an exception.
  • Concatenation - + converts automatically when joining a number to a string.
  • String.format() - converts and formats in one step, including zero padding with %05d.
  • No casting - (String) number does not compile; int to String always goes through a method.

The same conversion in the other direction, and in other languages, follows similar patterns. To compare, read our lesson on converting an int to a string in Python.

Anubhav Agarwal
About the author

Anubhav Agarwal

I'm a research-oriented engaged in various technologies and a technical content writer. Being a coder myself, going through the documentation to provide optimized solutions as technical content is what I always look for. Connect on LinkedIn →