Java Programs›Basics & I/O›Convert int to String

Convert int to String in Java

beginner·  Basics & I/O  ·  Type Conversion

Problem

Converting a number to a String produces its text form — the characters you would see when printing it — so it can be joined, measured or stored as text.

Given an int, convert it to a String in three different ways and confirm the results are equal.

Input
907
Output
"907"

Java Program

Java
public class IntToString { public static void main(String[] args) { int score = 907; String a = String.valueOf(score); // works for every primitive type String b = Integer.toString(score); // the method valueOf calls for an int String c = "" + score; // concatenation also converts, but builds an extra string System.out.println(a + " | " + b + " | " + c); System.out.println("Length of the text: " + a.length()); System.out.println("All equal: " + (a.equals(b) && b.equals(c))); } }

Output

907 | 907 | 907 Length of the text: 3 All equal: true

Core Logic

All three techniques produce the same text; they differ only in readability and in how much extra work happens behind the scenes.

How It Works
  1. 1String.valueOf(score) has overloads for every primitive type, which makes it a safe default.
  2. 2Integer.toString(score) is the method that valueOf calls for an int.
  3. 3"" + score converts through string concatenation, which builds an extra temporary string.
  4. 4a.length() works only because a is now text: 907 has three characters.
All three variables hold the text "907", so equals reports true.
💡

Key Point: Compare the converted strings with equals, never == — == checks whether two references point to the same object, not whether the text matches.

Key Concepts

String.valueOf()Integer.toString()string concatenationequals()

Approach 2: Manual Conversion (Digit by Digit)

Java
public class IntToStringManual { public static void main(String[] args) { System.out.println(toText(907)); System.out.println(toText(-4120)); System.out.println(toText(0)); } static String toText(int number) { if (number == 0) return "0"; boolean negative = number < 0; long n = Math.abs((long) number); // long, so Integer.MIN_VALUE doesn't overflow StringBuilder digits = new StringBuilder(); while (n > 0) { digits.append((char) ('0' + n % 10)); // last digit first n /= 10; } if (negative) digits.append('-'); return digits.reverse().toString(); } }

Output

907 -4120 0

Core Logic

Repeatedly taking n % 10 extracts digits from right to left, and '0' + digit turns each one into its character.

How It Works
  1. 1Zero is handled up front, because the loop would produce an empty string for it.
  2. 2The sign is recorded, and the absolute value is taken as a long so Integer.MIN_VALUE doesn't overflow.
  3. 3(char) ('0' + n % 10) appends the last digit as a character, then n /= 10 drops it.
  4. 4A minus sign is added if needed, and the builder is reversed.
For -4120, the digits come out as 0, 2, 1, 4, the minus sign is appended, and reversing gives "-4120".
💡

Key Point: The long cast is the subtle part: Math.abs(Integer.MIN_VALUE) is still negative in int arithmetic, which would break the loop.

Complexity
Time Complexity: O(d)Space Complexity: O(d)

Why: The loop runs once per digit d, and the builder holds one character per digit.

Key Concepts

modulo operatorchar arithmeticStringBuilder

Interview Questions

What's the fastest way to convert an int to a String?

String.valueOf(n) and Integer.toString(n) are equivalent and efficient. "" + n works but creates an extra temporary string.

How do you pad a number with leading zeros?

String.format("%05d", 42) gives "00042".

Learn the Concept

Related Programs