Example 1: Java Program to Convert char to int
class Main {
public static void main(String[] args) {
// create char variables
char a = '5';
char b = 'c';
// convert char variables to int
// ASCII value of characters is assigned
int num1 = a;
int num2 = b;
// print the values
System.out.println(num1); // 53
System.out.println(num2); // 99
}
}
In the above example, we have char
type variables a and b. Note that we have assigned the char variables to int
variables.
Here, instead of the characters, the ASCII value of the characters are assigned to the int
variables. Hence, we get the value 53 (ASCII value of '5') and 99 (ASCII value of 'c') as output.
Example 2: char to int using getNumericValue() method
We can also use the getNumericValue()
method of the Character
class to convert the char
type variable into int
type.
class Main {
public static void main(String[] args) {
// create char variables
char a = '5';
char b = '9';
// convert char variables to int
// Use getNumericValue()
int num1 = Character.getNumericValue(a);
int num2 = Character.getNumericValue(b);
// print the numeric value of characters
System.out.println(num1); // 5
System.out.println(num2); // 9
}
}
Here, as we can see the getNumericValue()
method returns the numeric value of the character. The character '5' is converted into an integer 5 and the character '9' is converted into an integer 9.
To learn more about the getNumericValue() method, visit Java getNumericValue() (Official Oracle Documentation).
Example 3: char to int using parseInt() method
We can also use the parseInt()
method of the Integer
class to convert the char
type variable to an int
type.
class Main {
public static void main(String[] args) {
// create char variables
char a = '5';
char b = '9';
// convert char variables to int
// Use parseInt()
int num1 = Integer.parseInt(String.valueOf(a));
int num2 = Integer.parseInt(String.valueOf(b));
// print numeric value
System.out.println(num1); // 5
System.out.println(num2); // 9
}
}
Notice the expression,
Integer.parseInt(String.valueOf(a))
Here,
- String.valueOf(a) - converts the
char
type variable a into aString
- Integer.parseInt() - converts the string into an
int
Note: The Integer.parseInt()
method only works with string type variables. Hence, the character 'a' is converted into a String
.
Example 4: char to int by subtracting with '0'
In Java, we can also convert the character into an integer by subtracting it with character 0. For example,
class Main {
public static void main(String[] args) {
// create char variables
char a = '9';
char b = '3';
// convert char variables to int
// by subtracting with char 0
int num1 = a - '0';
int num2 = b - '0';
// print numeric value
System.out.println(num1); // 9
System.out.println(num2); // 3
}
}
In the above example, notice the line,
int num1 = a -'0';
Here, we have subtracted the character 'a' by the character '0'. In this case, the characters are converted into integers. And subtracting a value by zero gives the same value. That is, 9 - 0 = 9
.
Hence, we get the integer values 9 and 3 of the character '9' and '3' respectively.