Write a Java Program to convert int type variables to char

Java is a popular programming language used to develop robust and efficient applications.

One of the basic data types in Java is int, which is used to store integer values.

On the other hand, char is a data type used to store characters.

In this tutorial, we will discuss how to convert an int variable to a char variable in Java.


To convert an int variable to a char variable, we can use a typecasting technique.

Typecasting is the process of converting one data type to another data type.

In Java, we can perform typecasting by placing the data type in parentheses before the variable to be cast.

Here’s an example:

int num = 65;
char ch = (char) num;
System.out.println(ch);

In the above code, we first declare an integer variable num and assign it a value of 65.

Then, we declare a char variable ch and use typecasting to convert the integer value to a character.

Finally, we print the value of the character variable ch, which will output ‘A’.

Note that when we convert an integer to a character, the ASCII value of the integer is used to determine the character.

In the example above, the ASCII value of 65 corresponds to the character ‘A’.

We can also use the charAt() method of the String class to convert an int value to a character.

Here’s an example:

int num = 97;
char ch = "abcdefghijklmnopqrstuvwxyz".charAt(num - 97);
System.out.println(ch);

In the above code, we first declare an integer variable num and assign it a value of 97.

Then, we use the charAt() method to get the character at the specified index in the string “abcdefghijklmnopqrstuvwxyz”.

Since the index of ‘a’ in the string is 0, we subtract 97 from num to get the index of the character we want.

Finally, we print the value of the character variable ch, which will output ‘a’.


In conclusion, converting an int variable to a char variable in Java is a simple process that can be done using typecasting or the charAt() method.

Remember that the ASCII value of the integer is used to determine the character when converting an integer to a character.