In this code snippet, you will learn how to get the ASCII value of a character in Java.

ASCII code values in Java

The concept is easy, the program will read from the user a char value, find the ASCII representation of that char, then print it out to the console.

import java.util.Scanner;
public class Main
{
    public static void main(String[] args) {
    char ch;
    short asciiValue;

    Scanner Scanner = new Scanner (System.in);
    
    
      System.out.print ("Please enter a character: ");
      ch = Scanner.next ().charAt (0);

      asciiValue = (short) ch;
      
  System.out.println ("The ASCII value of " + ch + " is: " + asciiValue);      
      
 
    }
}

Output

Please enter a character: i
The ASCII value of i is: 105

 

Note: The ASCII value of a lower letter I and the upper letter ‘I’ are not the same.

 

Last modified: November 10, 2020

Author

Comments

Write a Reply or Comment

Your email address will not be published.