Java Convert int to char
We can convert int to char in java using typecasting. To convert higher data type into lower, we need to perform typecasting. Here, the ASCII character of integer value will be stored in the char variable.
To get the actual value in char variable, you can add ‘0’ with int variable. Alternatively, you can use Character.forDigit() method.
Java int to char Example: Typecasting
Let’s see the simple code to convert long to int in java.
Output:
A
But if you store 1, it will store ASCII character of given number which is start of heading which is not printable. So it will not print anything on the console.
Output:
If you add ‘0’ with int variable, it will return actual value in the char variable. The ASCII value of ‘0’ is 48. So, if you add 1 with 48, it becomes 49 which is equal to 1. The ASCII character of 49 is 1.
Output:
1
If you store integer value in a single quote, it will store actual character in char variable.
Output:
1
Java int to char Example: Character.forDigit()
To get the actual value, you can also use Character.forDigit() method.
Output:
1
To get the hexa value, use redix 16 in Character.forDigit() method.
Output:
a
As you can see in the output, the hexadecimal code of 10 is a.