Java Program to Find ASCII Value of a character
In this java program, you'll learn how to find ASCII value of a character and display the result in the console.
How to Find ASCII Value of a character in Java?
Sourcecode
/*
Author : CodersEditor.com
Program : How to Find ASCII Value of a character in Java?
*/
public class Main {
public static void main(String[] args) {
char characterInput = 'A';
int ascii = characterInput;
System.out.println("The Ascii value of " + characterInput + " is: " + ascii);
}
}
Output
The Ascii value of A is: 65
Explanation
- In this java program, the character "A" is stored in a variable of type char.
- we just assign the character variable to integer which would be converted by Java internally to the integer (ascii value).
- The result is displayed in the console using System.out.print function.
note
- Single quotes (') is used to declare characters in Java. Double quotes(") is used to declare strings in java.
- Alternatively, You can find the ascii value by casting the character to integer using (int)