Convert int to char java: In the previous article we have discussed Java Program to Convert int to Double
In this article we will see how to convert an int
type to char
type.
Program to Convert int to char
How to convert int to char in java: Let’s see some examples of int
type and char
type.
Example: int type int a =2; int b = 38;
Example: char type char a = 65; char b= 'Y';
Now, let’s see different ways to convert int to char.
Want to excel in java coding? Practice with these Java Programs examples with output and write any kind of easy or difficult programs in the java language
Method 1 : Java Program to Convert int to char Using typecasting
Convert int to char java: Integer can be converted to Character by using valueOf()
method, lets see how it works. In this method we are using typecasting to convert an integer type variable into the char type variable.
Approach :
- Take a
integer
value and store it in anint
variable ”input1
”. - Typecast the variable with
char
and store it in a char variableoutput
. - Then display the result .
Program :
import java.util.Scanner; public class Main { public static void main(String[] args) { // creating scanner object Scanner sc = new Scanner(System.in); // input an integer through scanner class System.out.print("Enter an Integer : "); int input1=sc.nextInt(); // converting to string char output =(char)input1 ; System.out.println("Converted Character is :" + output); }
Output: Enter an Integer : 65 Converted Character is :A
Method 2 : Java Program to Convert int to char Using forDigit() method
Java convert int to char: Integer can be converted to Character by using forDigit(), lets see how it actually works.
Here, 10 and 16 will be the radix values
for decimal and hexadecimal numbers respectively i.e. if the integer value is between 0 to 9, in this case we use 10 as radix value, if the int value is between 0 to 15, in this case we use 16, and so on.
Approach :
- Take an integer value and store it in an
int
variableinput1
. - Then pass that input1 variable as parameter to
forDigit()
method which will convert theint
variable tochar
value and return it . - Store that character value in a
char
variableoutput
. - Display the result .
Program :
import java.util.Scanner; public class Main { public static void main(String[] args) { // creating scanner object Scanner sc = new Scanner(System.in); // input an int through scanner class System.out.print("Enter an Integer : "); int input1=sc.nextInt(); // converting to char char output =Character.forDigit(input1, 16); ; System.out.println("Converted Character is :" + output); } }
Output : Enter a Integer : 11 Converted Character is : b
Method 3 : Java Program to Convert int to char By adding “0”
How to convert int to char java: Integer can be converted to Character by adding “0”. Lets see how it will work .
Here, the character ‘0’ will be convert into ASCII value 48 & The value 48 is added to the value of the input . Hence, we get our desire character as the output.
Approach :
- Take an integer value and store it in an
int
variableinput1
. - Typecast the variable with
char
by adding “0 ”and store it in achar
variableoutput
. - Display the result .
Program :
import java.util.Scanner; public class Main { public static void main(String[] args) { // creating scanner object Scanner sc = new Scanner(System.in); // input an integer through scanner class System.out.print("Enter an Integer : "); int input1=sc.nextInt(); // converting to character char output =(char)(input1 + '0'); System.out.println("Converted Character is :" + output); } }
Output : Enter an Integer : 6 Converted Character is :6
Are you wondering how to seek help from subject matter experts and learn the Java language? Go
with these Basic Java Programming Examples and try to code all of them on your own then check
with the exact code provided by expert programmers.
Related Java Program: