Find ASCII Value of a Character in Java
Read a character and print its ASCII value.
BeginnerModule 1: Basic Java ProgramsExample 4 of 20
find-ascii-value-of-a-character.java
1import java.util.Scanner;23public class Main {4 public static void main(String[] args) {5 Scanner sc = new Scanner(System.in);67 System.out.print("Enter a character: ");8 char ch = sc.next().charAt(0);910 int ascii = (int) ch;11 System.out.println("ASCII value of " + ch + " = " + ascii);1213 sc.close();14 }15}
Output
Enter a character: A ASCII value of A = 65
What's going on
In Java, char is internally stored as a number (Unicode code point).
Casting the char to int reveals its numeric ASCII/Unicode value.