Java Program to find ASCII value of a Character

Java Program to find ASCII value of a Character

ASCII is a code for representing English characters as numbers, each letter of english alphabets is assigned a number ranging from 0 to 127. For example, the ASCII code for uppercase P is 80.
In Java programming, we have two ways to find ASCII value of a character 1) By assigning a character to the int variable 2) By type casting character value as int
Lets write a program to understand how it works:

Example: Program to find ASCII code of a character

public class Demo {

public static void main(String[] args) {

char ch = 'P';
int asciiCode = ch;
// type casting char as int
int asciiValue = (int)ch;

System.out.println("ASCII value of "+ch+" is: " + asciiCode);
System.out.println("ASCII value of "+ch+" is: " + asciiValue);
}
}
Output:
ASCII value of P is: 80
ASCII value of P
is: 80

Post a Comment

Previous Post Next Post