Java Integer toString() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Integer

Definition

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"
CallWhat it does
Integer.toString(n)Decimal text of the int n.
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

ParameterDescription
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 ValueDescription
StringThe digits of the number, with a leading - when it is negative. Digits above 9 are lowercase letters.

Compare with Similar Methods

Other ways to turn an int into text:

MethodHow it differs
toBinaryString(n)Shows the 32 raw bits, so negative numbers look very different.
toHexString(n)Raw bits in base 16, with no minus sign.
parseInt(s, radix)The reverse: reads text in a given base back into an int.
toUnsignedString(n)Prints the same 32 bits as a non-negative number.

Java Examples

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));
    }
}

How It Works

  1. Decimal forms agree. The instance call and the static call both give "255".
  2. Other bases. 255 is 11111111 in binary, 377 in octal, and ff in hex, using lowercase letters.
  3. Base 36. 255 is 7 × 36 + 3, so the text is 73.
  4. 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());
    }
}

How It Works

  1. Number becomes text. toString gives "90210", so length() counts the digits, which is 5.
  2. Character arithmetic. Subtracting '0' from a digit character gives that digit as a number.
  3. The sum. 9 + 0 + 2 + 1 + 0 equals 12.
  4. Reversed text. StringBuilder.reverse() flips the characters, so the zero moves to the front and prints 01209.

Example 3 — Meet the Radix Fallback and the null Trap

See what happens with an invalid radix, the smallest int, and a null object.

Java
public class IntegerToStringTraps {
    public static void main(String[] args) {
        System.out.println("radix 99:   " + Integer.toString(10, 99));
        System.out.println("radix 1:    " + Integer.toString(10, 1));
        System.out.println("MIN, hex:   " + Integer.toString(Integer.MIN_VALUE, 16));

        Integer missing = null;
        System.out.println("valueOf:    " + String.valueOf(missing));
        try {
            System.out.println(missing.toString());
        } catch (NullPointerException e) {
            System.out.println("toString(): NullPointerException");
        }
    }
}

How It Works

  1. Bad radix means 10. A base of 99 or 1 is outside 2 to 36, so Java quietly uses base 10 and prints 10.
  2. The smallest int. Integer.MIN_VALUE in base 16 is -80000000, the minus sign followed by the digits of 2147483648.
  3. valueOf is null-safe. String.valueOf(missing) does not call a method on null, so it returns the text "null".
  4. 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".

Next: toUnsignedLong()

Widen an int to a long as an unsigned value.

toUnsignedLong() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

13 people found this page helpful