Java program to Convert Int to Char

Write a Java program to convert int to char or integer to character. In this programming language, 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

Java program to Convert Int to Char using 0

We can also convert the integer to a character by adding the character ‘0’.

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 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

About Suresh

Suresh is the founder of TutorialGateway and a freelance software developer. He specialized in Designing and Developing Windows and Web applications. The experience he gained in Programming and BI integration, and reporting tools translates into this blog. You can find him on Facebook or Twitter.