ASCII values of a character using Java

ASCII values of a character using java :

 

In Computer, character data is represented by using standardized numeric codes which have been developed. The most widely accepted code is American Standard Code for Information Interchange (ASCII). The ASCII values are associated with every character either it is alphabets, digits, or special characters. There are 128 characters which are associated with the numbers between the range 0 to 127.

The distribution is done in such a way that lowercase letters ranges in between 97 to 122, uppercase letters ranges in between 65 to 90 and other remaining are reserved for special characters.

In this article, we will create a java program to display the ASCII value of a character entered by the user.

ASCII values of a character using java

Working :

Step 1 : Ask the user to enter a character.

Step 2 : Store the character in char type variable and then copy the character in the integer data type variable with the help of typecasting.

Step 3 : Print the value of the Integer variable.



Code in Java :

// Java program to print ASCII values of a character

import java.util.Scanner;
public class val_ascii
{
public static void main(String[] args)
{
//scanner class object creation
Scanner sc=new Scanner(System.in);
//input from user
System.out.print("Enter a Character: ");
char c=sc.next().charAt(0);
//typecasting from character type to integer type
int i = c;
//printing ASCII value of the character
System.out.println("ASCII value of "+c+" is "+i);
//closing scanner class(not compulsory, but good practice)
sc.close();
}
}

Output :

Enter a Character: R

ASCII value of R is 82


Enter a Character: $


ASCII value of $ is 36