Find ASCII of a Character in Java

Exercise:

Write a Java Program that displays ASCII value of a character.

Click Here to View the Solution!
public class AsciiValueFinder {
    public static void main(String[] args) {
 
        char character = 'b';
        int ascii = character;
        // This will cast char to int
        int castAscii = (int) character;
 
        System.out.println("The ASCII value of " + character + " is: " + ascii);
        System.out.println("The ASCII value of " + character + " is: " + castAscii);
    }
} 
Click Here to View the Output!
The ASCII value of b is: 98
The ASCII value of b is: 98
Click Here to View the Explanation!
  • In this program we stored character b in a variable named character.
  • It’s important to notice that strings are declared in double quotes (" ") while single quotes (' ')  are used to declare character-data type.
  • Java internally converts the character value to ASCII . So to find the ASCII value of character, we just need to assign character to an int variable ascii.
  • Casting is the process of converting a variables of certain data type to another data type , So here we can also use casting method to find the ASCII value of character  by using int  castAscii .
  • Finally println() function prints  ascii  value on console screen.