HOWTO · Java

How to Convert Int to Char in Java

This tutorial shows how to convert int to char in Java by casting to char, using Character.forDigit(), or taking the first character of Integer.toString().

On this page

In Java, choose the operation by what the int means: cast with (char) when it is a UTF-16 code unit, use Character.forDigit() for one digit in a radix, or use Integer.toString() when you specifically need the first character of a decimal representation. These operations have different results and are not interchangeable.

Cast an int to a char

A cast treats the integer value as a UTF-16 code unit; it does not format the number as decimal text. For example, code unit 65 corresponds to A. To get the character 1, the integer must represent that character (as in the literal '1') or be a digit from 0 through 9 with '0' added.

Java’s char stores one 16-bit UTF-16 code unit. A cast from int to char narrows the value to that size, so it is useful when your input already represents a code unit. It is not a range check: values outside the char range do not produce an error, and the resulting code unit may not be the character you intended. If the value comes from user input or a file, validate it before casting when preserving its original numeric value matters.

This distinction also matters for Unicode. Many familiar symbols fit in one char, but a supplementary Unicode code point is represented by two UTF-16 code units, called a surrogate pair. One char cannot hold that full code point. Use a string or code-point-aware operation when your data may include such characters. For an ASCII decimal digit, adding '0' works only for values 0 through 9 because those ten digit characters have consecutive code-unit values; it is not a general number-to-text conversion.

Convert a digit with Character.forDigit()

Character.forDigit() maps one valid digit value to the character for the given radix. For radix 10, value 6 represents '6'; for radix 16, value 12 represents 'c'. The digit must be in the range from 0 up to, but not including, the radix, and the radix must be between 2 and 36. If either argument is outside its valid range, the method returns the NUL character (\u0000), not null.

Use this method when the integer is one digit, rather than a complete number to print. For example, hexadecimal has radix 16 and uses values 0 through 15; values 10 through 15 map to the letters a through f. The returned letter is lowercase. If your output format calls for uppercase hexadecimal, convert the returned character with Character.toUpperCase() after choosing a valid digit. The inverse helper, Character.digit(character, radix), can turn a radix digit back into its numeric value.

The NUL result is a char whose value is \u0000; it is not the Java reference null, and many terminals display it as nothing. When input can come from outside your program, check the radix and digit ranges yourself so that this result is not mistaken for a successful conversion. For a whole number such as 42, convert the complete value with a formatting or string operation instead of calling Character.forDigit() once.

Get the first character of Integer.toString()

Integer.toString(value).charAt(0) converts the integer to its decimal string and selects only the first character. With value 1, that character is '1'. For a multi-digit value this returns its leading character, not a char representing the whole number.

For example, a value of 42 produces the string "42", so charAt(0) returns '4'. A negative value begins with a minus sign, so this expression returns '-' for a value such as -42. This approach is appropriate when the requirement really is to inspect or display the first character of the decimal representation. If you need every digit, keep the result as a String; a single char cannot represent a multi-character number.

Choose by the meaning of the input: use a cast for a UTF-16 code unit, Character.forDigit() for one digit in a selected radix, and Integer.toString() plus charAt(0) for the leading character of decimal text. Keeping that distinction explicit prevents a numeric value such as 65 from being confused with the two printed characters in "65".

The program below shows all three operations with the values described above. It prints the resulting characters in the same order as the operations are introduced.

public class IntToCharExample {
    public static void main(String[] args) {
        int codeUnit = 65;
        char letter = (char) codeUnit;

        int literalDigitValue = '1';
        char literalDigit = (char) literalDigitValue;
        int digit = 1;
        char addedZeroDigit = (char) (digit + '0');

        char decimalDigit = Character.forDigit(6, 10);
        char hexadecimalDigit = Character.forDigit(12, 16);

        int value = 1;
        char firstCharacter = Integer.toString(value).charAt(0);

        System.out.println(letter);
        System.out.println(literalDigit);
        System.out.println(addedZeroDigit);
        System.out.println(decimalDigit);
        System.out.println(hexadecimalDigit);
        System.out.println(firstCharacter);
    }
}
A
1
1
6
c
1