Convert Char to Int in Java
Hassan Saeed
Jan 30, 2023
Oct 01, 2020
-
Use
Character.getNumericValue(ch)
to Convert achar
to anint
-
Subtract the Given
char
From0
in Java

This tutorial discusses methods to convert a char
to an int
in Java.
Use Character.getNumericValue(ch)
to Convert a char
to an int
The simplest way to convert a char
to an int
in Java is to use the built-in function of the Character
class - Character.getNumericValue(ch)
. The below example illustrates this:
public class MyClass {
public static void main(String args[]) {
char myChar = '5';
int myInt = Character.getNumericValue(myChar);
System.out.println("Value after conversion to int: " + myInt);
}
}
Output:
Value after conversion to int: 5
Subtract the Given char
From 0
in Java
Another way to convert a char
to an int
is to subtract it from the char 0
. The below example illustrates this:
public class MyClass {
public static void main(String args[]) {
char myChar = '5';
int myInt = myChar - '0';
System.out.println("Value after conversion to int: " + myInt);
}
}
Output:
Value after conversion to int: 5
These are the two commonly used methods to convert a char
to an int
in Java. However, keep in mind that even if the given char
does not represent a valid digit, the above methods will not give any error. Consider the example below.
public class MyClass {
public static void main(String args[]) {
char myChar = 'A';
int myInt = myChar - '0';
System.out.println("Value after conversion to int: " + myInt);
}
}
Output:
Value after conversion to int: 17
Even though the input char
was an alphabet, the code ran successfully. Therefore, we must ensure that the input char
represents a valid digit.
Related Article - Java Int
- Convert Int to Char in Java
- Convert Int to Double in Java
- Convert Object to Int in Java
- List of Ints in Java
- Convert Integer to Int in Java
- Check if Int Is Null in Java
Related Article - Java Char
- Convert Int to Char in Java
- Char vs String in Java
- Initialize Char in Java
- Represent Empty Char in Java
- Char to Uppercase/Lowercase in Java
- Check if a Character Is Alphanumeric in Java