Java Program to Find ASCII Value of a Character

In this post, we will learn how to find the ASCII value of a character using Java Programming language.

ASCII stands for American Standard Code for Information Interchange is a character encoding that uses numeric codes to represent characters. These include upper and lowercase English letters, numbers and punctuation symbols.

In this program, we will know two ways to find the ascii value of a character.

  1. By assigning a character to int variable
  2. By type casting character value as int

So, without further ado, let’s begin this tutorial.

Contents

Java Program to Find ASCII Value of a Character

// Java Program to Find ASCII Value of a Character
public class ASCIIValue {
    public static void main(String[] args){
        char ch = 'N';
        int asciicode = ch;
        int asciivalue = (int)ch;
        
        // Displaying output
        System.out.println("ASCII Value of " + ch + " is: " + asciicode);
        System.out.println("ASCII Value of " + ch + " is: " + asciivalue);
    }
}

Output

ASCII Value of N is: 78
ASCII Value of N is: 78

How Does This Program Work ?

        char ch = 'N';

In this program, we have declared a character data type variable named ch. This variable is assigned a character N.

       int asciicode = ch;

Then, we find the ascii value of a character by assigning the character to an int variable.

        int asciivalue = (int)ch;

In this case, we find the ascii value by type casting character value as int.

       // Displaying output
        System.out.println("ASCII Value of " + ch + " is: " + asciicode);
        System.out.println("ASCII Value of " + ch + " is: " + asciivalue);

Finally, the output is displayed on the screen using System.out.println() function.

Conclusion

I hope after going through this post, you understand how to find the ASCII value of a character using Java Programming language.

If you have any doubt regarding the program, feel free to contact us in the comment section. We will be delighted to help you.

Also Read:

Leave a Comment

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