In Java, toString turns a number into text. Integer.toString(n) is the static form for a primitive int, and boxed.toString() is the instance form for an Integer object. Both give plain decimal digits.
The two-argument form Integer.toString(n, radix) writes the number in another base from 2 to 36, using lowercase letters for digits above 9. Negative numbers get a leading minus sign, never a two's complement pattern.
Available both as an instance method and as a static method of java.lang.Integer. Integer lives in java.lang, so no import is needed.
Syntax
Use the static form with an int, or the instance form on an Integer.
Java
String a = Integer.toString(255); // "255"
String b = Integer.toString(255, 16); // "ff"
Integer boxed = 255;
String c = boxed.toString(); // "255"
Call
What it does
Integer.toString(n)
Decimal text of the intn.
Integer.toString(n, radix)
Text of n in the given base, from 2 to 36.
boxed.toString()
Decimal text of an Integer object.
String.valueOf(n)
Same text; also safe when the argument might be null.
"" + n and String.valueOf(n) give the same decimal text as Integer.toString(n). Use toString(n, radix) when you need another base.
Library declarations
Java
// java.lang.Integer
public String toString() // instance
public static String toString(int i) // static
public static String toString(int i, int radix) // static
Parameters
Parameter
Description
i (int)
The number to convert. Only the static forms take it; the instance form uses the object itself.
radix (int)
The base, from 2 to 36. A value outside that range silently falls back to 10.
Return Value
Return Value
Description
String
The digits of the number, with a leading - when it is negative. Digits above 9 are lowercase letters.
Example 1 — Convert a Number to Text in Several Ways
Use the instance form, the static form, and the radix form on the same number.
Java
public class IntegerToStringDemo {
public static void main(String[] args) {
Integer boxed = 255;
int n = 255;
System.out.println("instance: " + boxed.toString());
System.out.println("static: " + Integer.toString(n));
System.out.println("base 2: " + Integer.toString(n, 2));
System.out.println("base 8: " + Integer.toString(n, 8));
System.out.println("base 16: " + Integer.toString(n, 16));
System.out.println("base 36: " + Integer.toString(n, 36));
System.out.println("negative base 16: " + Integer.toString(-n, 16));
}
}
Output
instance: 255
static: 255
base 2: 11111111
base 8: 377
base 16: ff
base 36: 73
negative base 16: -ff
How It Works
Decimal forms agree. The instance call and the static call both give "255".
Other bases. 255 is 11111111 in binary, 377 in octal, and ff in hex, using lowercase letters.
Base 36. 255 is 7 × 36 + 3, so the text is 73.
The sign stays outside. A negative number keeps a leading minus and then writes the digits of its positive twin: -ff.
Example 2 — Count, Sum, and Reverse the Digits of a Number
Turn a number into text so you can look at each digit.
Java
public class IntegerToStringDigits {
public static void main(String[] args) {
int number = 90210;
String text = Integer.toString(number);
int sum = 0;
for (int i = 0; i < text.length(); i++) {
sum += text.charAt(i) - '0';
}
System.out.println("text: " + text);
System.out.println("digits: " + text.length());
System.out.println("digit sum: " + sum);
System.out.println("reversed: " + new StringBuilder(text).reverse());
}
}
Bad radix means 10. A base of 99 or 1 is outside 2 to 36, so Java quietly uses base 10 and prints 10.
The smallest int.Integer.MIN_VALUE in base 16 is -80000000, the minus sign followed by the digits of 2147483648.
valueOf is null-safe.String.valueOf(missing) does not call a method on null, so it returns the text "null".
toString() is not. Calling the instance method on a null variable throws NullPointerException.
Frequently Asked Questions
Call Integer.toString(n), or String.valueOf(n), or join it to text with "" + n. All three give the same decimal digits.
Use Integer.toString(n, 2). For negative numbers it returns a minus sign and the positive digits, so use Integer.toBinaryString(n) when you want the raw 32 bits.
The static form takes a primitive int. The instance form works on an Integer object and throws NullPointerException if the object is null.
A radix below 2 or above 36 does not throw an exception. Java replaces it with 10.
Lower case. Integer.toString(255, 16) returns "ff". Call toUpperCase() on the result if you need "FF".