How to Convert char to int in Java
How to Convert char to int in Java
When two variables of different types are involved in the single expression, Java compiler uses built-in library function to convert the variable to common data type before evaluating the expression. Java compiler follows certain rules to decide which variable’s data type is to be converted. It converts the lower data type to that of a higher data type implicitly. int occupies 4 bytes in the memory and char occupies 2 bytes.
When we perform the conversion from char to int, we get ASCII value (integer value) of the given character. It is called implicit typecasting or widening conversion or type promotion.
There are three ways to convert char to int.
- Using implicit typecasting
- Using getNumricValue() method
- Using String.valueOf() method
Valid conversions
char ch='a';
char ch='A'; //alphabets
char ch='1'; //digits
char ch='@'; //special characters
char ch=' '; //space (null character)
Using implicit typecasting
We are assigning char values to integer variables without any typecasting. The compiler automatically performs the conversion.
Example
In the following example, we have taken two variable ch1 and ch2 of char type and initialize 'z' and ‘>' into these variables respectively. There are two integer variables num1 and num2. We have assigned ch1 and ch2 into the variable num1 and num2, respectively. The first println statement prints the ASCII value of the character ch1, and the second println statement prints the ASCII value of the character ch2.
public class charTointExample { public static void main(String args[]) { char ch1='z'; char ch2='>'; int num1=ch1; int num2=ch2; System.out.println ("ASCII value of” +ch1+ " is: " +num1); System.out.println ("ASCII value of” +ch2+ " is: " +num2); } }
Output
ASCII value of z is: 122 ASCII value of > is: 62
Using getNumericValue() method
You can also use getNumericValue(char) method to convert char to int. This is the static method of Character class. It accepts char as an argument and returns equivalent int value which denotes the numeric value of the character. It returns -1 if the character contains space (null character) or any special character. The signature of the method is given below.
public static int getNumericValue(char ch)
Where ch is a character to be converted.
Example
In the following example, we have taken a variable ch of type char and initialize 'P' to it. num is a variable of type int that stores the numeric value of the character. Character is a class that invokes the getNumericValue() method. This method parses character as an argument and returns the corresponding numeric value. The println statement prints the numeric value of the character ch.
public class charTointExample1 { public static void main(String args[]) { char ch='P'; int num=Character.getNumericValue(ch); System.out.println("Numeric value of " +ch+ " is: " +num); } }
Output
Numeric value of P is: 25
Using String.valueOf() method
The valueOf() is a method of String class. It accepts the character as an argument and returns the integer value of specified char value.
Remember: This method does not accept any special character and space (null character). Only alphabets and digits are allowed. If you initialize special character or space as a character, then it throws NumberFormatException.
Example
In the following example, we have taken a variable ch of type char and initialized 4 to it. Here, num is a variable of type int that stores the converted int value of char. String.valueOf(ch) method converts the char into String. Integer.parseInt() method parses the String as an argument and returns the corresponding integer value. The println statement prints the integer value of the character ch.
public class charTointExample2 { public static void main(String args[]) { char ch='4'; int num=Integer.parseInt(String.valueOf(ch)); System.out.println("Integer value of character is: " +num); } }
Output
Integer value of character is: 4