Java Top

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

> CHECK OUT THE COURSE

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.

Java bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE
Generic footer banner
Comments are closed on this article!