Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
76 views

Java Program To Find ASCII Value of A Character

This Java program demonstrates two ways to get the ASCII value of a character. It defines a char variable ch with value 'a' and assigns ch to an int variable ascii to get the ASCII value of 97 without casting. It also casts ch to an int and assigns it to castAscii, printing both ASCII values. The output shows the ASCII value of 'a' is 97 from either method.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views

Java Program To Find ASCII Value of A Character

This Java program demonstrates two ways to get the ASCII value of a character. It defines a char variable ch with value 'a' and assigns ch to an int variable ascii to get the ASCII value of 97 without casting. It also casts ch to an int and assigns it to castAscii, printing both ASCII values. The output shows the ASCII value of 'a' is 97 from either method.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Example: Find ASCII value of a character

public class AsciiValue {

public static void main(String[] args) {

char ch = 'a';
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: 97


The ASCII value of a is: 97

In the above program, character a is stored in a char variable, ch . Like, double


quotes (" ") are used to declare strings, we use single quotes (' ') to declare
characters.

Now, to find the ASCII value of ch , we just assign ch to an int variable ascii .
Internally, Java converts the character 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 .

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

You might also like