java

How to Get the ASCII Value of a Character in Java

In this tutorial, we are going to see how to get the ASCII value of a character in Java.

ASCII is a code to represent English characters as numbers, each letter of the English alphabet is assigned a number from 0 to 127. For example, the ASCII code for uppercase A is 65.
 

Method 1: Assignment operator
public class Main {

    public static void main(String[] args) {

        char ch = 'A';
        int ascii = ch;

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

Output:

The ASCII value of A is: 65

 

 

Method 2: Type casting
public class Main {

    public static void main(String[] args) {

        char ch = 'A';
        int ascii = (int)ch;

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

Output:

The ASCII value of A is: 65
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *