DEV Community

Lalit Kumar
Lalit Kumar

Posted on

How to get ASCII value of char in java?

In this article,we will learn about how to get ascii value of char in java.

ASCII is a code for representing English characters as numbers, each letter of the English alphabet is assigned a number ranging from 0 to 127. For example, the ASCII code for uppercase P is 80.


title: "originally posted here πŸ‘‡"

canonical_url: https://kodlogs.com/blog/2183/how-to-get-ascii-value-of-char-in-java

In Java programming, we have two ways to find the ASCII value of the character 1) By assigning characters to int variable 2) With a value of type casting character as an int

Let us write a program to understand how it works:

Example: Program to find the ASCII code of character

public class Demo {

    public static void main (String [] args) {

        Char ch = 'P';

        int asciiCode = ch;

        // char type casting int

        asciiValue int = (int) ch;



        System.out.println ( "ASCII value of" + ch + "is:" + asciiCode);

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

    }

}
Enter fullscreen mode Exit fullscreen mode

Output:

ASCII value of P is: 80

ASCII value of P is: 80

Top comments (0)