Of course! Converting a char to an int in Java is a very common task. There are two primary ways to do it, and it's crucial to understand the difference between them.
The Short Answer (TL;DR)
The most direct and common way to get the integer value of the character (e.g., 'A' -> 65) is to cast the char to an int.
char myChar = 'A'; int myInt = (int) myChar; // myInt will be 65
Method 1: Casting to int (Get the ASCII/Unicode Value)
This is the standard way to get the underlying numeric value of a char. In Java, the char data type is a 16-bit unsigned integer representing a character in the Unicode standard. Casting it to an int simply reveals that integer value.
How it Works
When you cast a char to an int, Java performs an implicit widening primitive conversion. It doesn't change the data; it just allows you to view the char's numeric value as an int.
Example Code
public class CharToInt {
public static void main(String[] args) {
char digitChar = '9';
char upperChar = 'Z';
char lowerChar = 'z';
char specialChar = '@';
// Get the Unicode code point for each character
int digitValue = (int) digitChar;
int upperValue = (int) upperChar;
int lowerValue = (int) lowerChar;
int specialValue = (int) specialChar;
System.out.println("The character '" + digitChar + "' has a value of: " + digitValue);
System.out.println("The character '" + upperChar + "' has a value of: " + upperValue);
System.out.println("The character '" + lowerChar + "' has a value of: " + lowerValue);
System.out.println("The character '" + specialChar + "' has a value of: " + specialValue);
}
}
Output
The character '9' has a value of: 57
The character 'Z' has a value of: 90
The character 'z' has a value of: 122
The character '@' has a value of: 64
As you can see, '9' is not converted to the integer 9, but to its Unicode code point, which is 57.

Method 2: Get the Numeric Value of a Digit Character
If you have a char that represents a digit (e.g., '5') and you want to get its actual integer value (e.g., 5), casting won't work. You need to perform a mathematical operation.

How it Works
You can subtract the character '0' from your digit character. Since both are chars, Java calculates the difference between their Unicode code points. The code point for '0' is 48, for '1' is 49, and so on. The difference is exactly the integer you want.

'5' - '0' results in 53 - 48 = 5.
Example Code
public class CharDigitToInt {
public static void main(String[] args) {
char digitChar = '5';
// Subtract the character '0' to get the numeric value
int numericValue = digitChar - '0';
System.out.println("The character '" + digitChar + "' represents the integer: " + numericValue);
// Another example with a different digit
char anotherDigit = '9';
int anotherValue = anotherDigit - '0';
System.out.println("The character '" + anotherDigit + "' represents the integer: " + anotherValue);
}
}
Output
The character '5' represents the integer: 5
The character '9' represents the integer: 9
Summary Table
| Method | When to Use | Example | Result |
|---|---|---|---|
Casting (int) |
When you want the character's Unicode code point. | (int) 'A' |
65 |
Subtracting '0' |
When you have a digit character ('0'-'9') and want its numeric value. |
'7' - '0' |
7 |
Important Consideration: Character.getNumericValue()
Java also provides a built-in method in the Character class that can handle digit conversion, including more complex cases like non-ASCII digits (e.g., Arabic, Tamil).
char digitChar = '5'; int numericValue = Character.getNumericValue(digitChar); System.out.println(numericValue); // Output: 5 // Example with a non-ASCII digit (e.g., Devanagari digit five) char devanagariFive = '५'; // This is the character for '5' in Devanagari script int devanagariValue = Character.getNumericValue(devanagariFive); System.out.println(devanagariValue); // Output: 5
When to use Character.getNumericValue()?
- When your code needs to be internationalized and handle digits from various languages.
- It's more robust and readable for the specific purpose of getting a digit's value.
For simple, ASCII-based digits, digitChar - '0' is perfectly fine and very common.
