If you have a few years of experience in the Java ecosystem and you'd like to share that with the community, have a look at our Contribution Guidelines.
Get the ASCII Value of a Character in Java
Last modified: May 4, 2022
1. Overview
In this short tutorial, we'll see how to get the ASCII value of a character in Java.
2. Use Casting
To get the ASCII value of a character, we can simply cast our char as an int:
char c = 'a';
System.out.println((int) c);
And here is the output:
97
Remember that char in Java can be a Unicode character. So our character must be an ASCII character to be able to get its correct ASCII numeric value.
3. Character Inside a String
If our char is in a String, we can use the charAt() method to retrieve it:
String str = "abc";
char c = str.charAt(0);
System.out.println((int) c);
4. Conclusion
In summary, we've learned how to get the ASCII value of a character in Java.