Java program to find ASCII value of a character

Write a java program to find ASCII value of a character

In this program, find and display the ASCII value of a character in java using typecasting and normal variable assignment operations.

Here, character n is stored in a char variable ch. Like, single quotes (' ') are used to declare characters, we use double quotes (" ") are used to declare strings.

Now, to find the ASCII value of ch, we just assign ch to an int variable ASCII. Internally, Java converts the characters value to an ASCII value.

We can also cast the character ch to an integer using int. In simple terms, casting is converting variable from one type to another, here char variable ch is converted to an int variable castAscii

Last, we print the ascii value using the println() function.

  • Program:


public class AsciiValue {

    public static void main(String[] args) {

        char ch = 'n';
        int ASCII = ch;
        // You can also cast char to int
        int castAscii = (int) ch;

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

  • Output:


The ASCII value of a is: 110
The ASCII value of a is: 110

  • Visit:

Java program to print an Integer Here
Java program to add two integers Here

0 comments