Java Convert int to char

In this tutorial we will see java convert int to char example . To convert a higher data type into a lower data type in java, we needs to be performed typecasting. Firstly here we shall manually convert a value of a higher datatype to a value of lower datatype.

Secondly, we will discuss about Character.forDigit() method, which is used to get the character representation of a number for a given radix. and after that we shall discuss about how to use toString() and charAt() method for converting integer to character in java .

Java Convert int to char – Narrow Casting

As we knew , In java int is higher data type as compared to char data type . So when we convert higher datatype to lower data type , we have manually cast int to char . This is also called narrowing primitive conversion or narrowing casting. In this example we take int type variable and do narrowing casting using (char) intvalue . Let,s see java convert int to char example using typecasting .

public class IntToChar {
	
	public static void main(String[] args) {
		int num1 = 69;  
		char ch1 = (char)num1;  
		System.out.println(ch1);  

		int num2 = 97;    
		char ch2 = (char)num2;   
		System.out.println(ch2); 

		int num3 = 101;    
		char ch3 = (char)num3;  
		System.out.println(ch3); 
	}

}

Output :

E
a
e

If we put an integer value in a single quote, then it will store the actual character in the char variable (values should be between 0 to 9)

public class IntToChar1 {
	
	public static void main(String[] args) {
		int num1 = '5';  
		char ch1 = (char)num1;  
		System.out.println(ch1);  	
	}
}

Output :

5

Convert int to char Using Character.forDigit()

In this example we will use Character.forDigit() method to convert int to char in java . Character.forDigit() is a method of Character class that returns a char value for the character determined by the digit (first argument) and radix (second argument) otherwise It returns null if the value of the radix or digit is invalid.

For example, the digit representation of 11 in hexa-decimal (radix 16) system is b.

public class IntToChar2 {
	
	public static void main(String[] args) {
		
		int digit1 = 11;  
		int radix1 = 16;
		char ch1 = Character.forDigit(digit1, radix1);
		System.out.println(ch1);  
		
		int digit2 = 8;  
		int radix2 = 10;
		char ch2 = Character.forDigit(digit2, radix2);
		System.out.println(ch2);  
				
	}

}

Output:

b
8

Java convert int to char using toString() and charAt() method

In this example we are converting int to char in java using toString() and charAt() method . Let,s see java program to convert int to char using these toString() and charAt() method.

public class IntToChar3 {
	
	public static void main(String[] args) {
		
		int num1 = 1;  
		
		char ch1 = Integer.toString(num1).charAt(0);;  
		
		System.out.println(ch1);  
		
	}

}

Output :

1

In this tutorial we have learned how to convert int to char in java . We have discuss different approaches to java convert int to char with examples . you can see more good java program for practice .

Leave a Reply

Your email address will not be published. Required fields are marked *

17 − = 10