Java How to Convert int to char with Examples
In Java, you can convert an
int to a char by casting it like this: char c = (char) intValue; where intValue is the integer you want to convert.Examples
Input65
OutputA
Input97
Outputa
Input48
Output0
How to Think About It
To convert an integer to a character, think of the integer as a code that represents a symbol in the character set. By casting the integer to a char, Java treats the number as the Unicode code point and gives you the matching character.
Algorithm
1
Get the integer value you want to convert.2
Cast the integer to a char type using (char).3
Use or print the resulting char value.Code
java
public class IntToChar { public static void main(String[] args) { int intValue = 65; char charValue = (char) intValue; System.out.println(charValue); } }
Output
A
Dry Run
Let's trace converting int 65 to char through the code
1
Assign int value
intValue = 65
2
Cast int to char
charValue = (char) 65
3
Print char
Output: 'A'
| intValue | charValue |
|---|---|
| 65 | A |
Why This Works
Step 1: Casting int to char
The (char) cast tells Java to treat the integer as a Unicode code point.
Step 2: Unicode mapping
Each integer corresponds to a character in Unicode, so 65 maps to 'A'.
Step 3: Resulting character
The casted value is stored as a char and can be printed or used as a character.
Alternative Approaches
Using Character.toChars()
java
public class IntToCharAlt { public static void main(String[] args) { int intValue = 65; char[] chars = Character.toChars(intValue); System.out.println(chars[0]); } }
This method returns a char array and is useful for code points outside the basic multilingual plane.
Using String.valueOf()
java
public class IntToCharString { public static void main(String[] args) { int intValue = 65; String s = String.valueOf((char) intValue); System.out.println(s); } }
Converts int to char then to String; useful when you need a String instead of a char.
Complexity: O(1) time, O(1) space
Time Complexity
Casting an int to char is a direct operation with no loops, so it runs in constant time.
Space Complexity
No extra memory is needed beyond storing the char, so space usage is constant.
Which Approach is Fastest?
Simple casting is the fastest and most straightforward method; alternatives add overhead and are used for special cases.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Casting (char) | O(1) | O(1) | Simple and fast conversions |
| Character.toChars() | O(1) | O(1) | Handling Unicode code points beyond 16-bit chars |
| String.valueOf() | O(1) | O(1) | When a String result is needed |
Use simple casting
(char) for most int to char conversions in Java.Forgetting to cast the int to char and trying to assign directly causes a compile error.