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:
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);
In summary, we've learned how to get the ASCII value of a character in Java.