
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program for Int to Char Conversion
The given task is to write a Java program that converts an integer into a character. The int and char are primitive datatypes of Java.
The int is a 32-bit signed datatype used to store whole numbers, and char holds 16-bit unsigned Unicode characters.
The int and char keywords are used to declare an integer variable and a character variable, respectively. We store character variables in a single quotation (' ').
Example Scenario
To understand the problem statement, let's see an example scenario:
Input: int num1 = 83; Output: ch = 'S'
Here, num1 is the name of a variable that holds the value 83. When we convert this integer variable to a character, it will print 'S'. Because the ASCII value of S is 83.
"int" to "char" Conversion in Java
Every character variable in Java has its Unicode value represented by an integer value. During the "int" to "char" conversion, we get the character value of the given integer. We will use the following ways in our Java programs to convert an int to a char:
-
By adding '0'
-
Using forDigit() method
-
Using typecasting
Using forDigit() Method
The forDigit() method of class Character returns a character for the specified integer according to the given RADIX value. The RADIX value for decimal is 10, for hexadecimal it is 16, and for binary it is 2.
We will also use getClass().getSimpleName() method in our program to check the datatype of given int variables after converting to a character. The forDigit() method does not return an ASCII value.
Syntax of forDigit()
char char_variable = Character.forDigit(var_to_convert, radix);
Example
In the below code, we are using a radix value of 16 so that the specified variable should change to a hexadecimal numeric system (0 to 9 and after 9 A to F).
import java.lang.*; public class Conversion { public static void main(String[] args) { int rad = 16; int n1 = 8; int n2 = 11; // Conversion of int to char char ch1 = Character.forDigit(n1, rad ); char ch2 = Character.forDigit(n2, rad ); System.out.println("ch1: " + ch1); System.out.println("ch2: " + ch2); // to check the datatype System.out.println("type of ch1: " + ((Object)ch1).getClass().getSimpleName()); System.out.println("type of ch2: " + ((Object)ch2).getClass().getSimpleName()); } }
On running the above Java program, it will print the character value after converting the integer:
ch1: 8 ch2: b type of ch1: Character type of ch2: Character
Using Typecasting
When we convert a data type to another data type, we call it type casting. There are two types of type casting in Java, explicit and implicit. In implicit typecasting, a lower datatype is converted to any other higher datatype automatically by the compiler.
When we typecast a higher datatype to a lower data type, then it is called explicit type casting, and it is done manually. In our program, we will use explicit typecasting because the integer range is higher than the character.
To typecast, we use the following syntax:
char_variable = (char) var_to_convert;
Example
In the Java program given below, we are typecasting an integer value to a character.
public class Conversion { public static void main(String[] args) { int n1 = 97; int n2 = 65; // Typecasting of int to char variable char ch1 = (char)n1; char ch2 = (char)n2; System.out.println("ch1: " + ch1); System.out.println("ch2: " + ch2); } }
When you execute, you will get 'a' and 'A' in output because they are the ASCII values of 65 and 97.
ch1: a ch2: A
By adding '0'
When we add 0 to an integer variable and typecast it, then it will actually return the ASCII value of that digit. For example, 0 has the ASCII value of 48, so if we add 65 to 0, it will become 113, and the ASCII value of 113 is q.
Example
Let's see a practical example:
public class Conversion { public static void main(String[] args) { int n1 = 65; int n2 = 66; // Conversion of int to char char ch1 = (char)(n1 + '0'); char ch2 = (char)(n2 + '0'); System.out.println("ch1: " + ch1); System.out.println("ch2: " + ch2); System.out.println("type of ch1: " + ((Object)ch1).getClass().getSimpleName()); System.out.println("type of ch2: " + ((Object)ch2).getClass().getSimpleName()); } }
Output:
ch1: q ch2: r type of ch1: Character type of ch2: Character