Java program to Convert Int to Char

Write a Java program to convert int to char or integer to character. In Java, we have to explicitly convert the integer to a character because we convert the 4 bytes integer to 2-byte characters. This technique considers the integer as the ASCII value and returns the equivalent character.

package NumPrograms;

import java.util.Scanner;

public class IntToChar1 {
	private static Scanner sc;	
	
	public static void main(String[] args) {
		
		sc= new Scanner(System.in);	
		
		System.out.print("Enter Any Integer Value =  ");
		int i = sc.nextInt();
		
		char ch = (char)i;

		System.out.println("Integer to Character = " + ch);
	}
}
Java program to Convert Int to Char

In Java, we can also convert the integer to a character by adding the character ‘0’ to it.

package NumPrograms;

public class IntToChar2 {
	
	public static void main(String[] args) {
		
		int i = 4;
		int j = 9;
		
		char ch1 = (char)(i + '0');
		char ch2 = (char)(j + '0');
		
		System.out.println(ch1);
		System.out.println(ch2);
	}
}
4
9

This Java example uses the Character forDigit method to convert int to char. For decimal values, the second argument will be 10, and for Hexa decimals, it has to be 16.

package NumPrograms;

public class IntToChar3 {
	
	public static void main(String[] args) {
		
		int i = 9;
		int j = 14;
		int k = 1;
		
		char ch1 = Character.forDigit(i, 10);
		char ch2 = Character.forDigit(j, 16);
		char ch3 = Character.forDigit(k, 10);
		
		System.out.println(ch1);
		System.out.println(ch2);
		System.out.println(ch3);
	}
}
9
e
1