;

Java Program to Find ASCII Value of a character


Tutorialsrack 19/04/2021 Java

In this java program, you’ll learn how to find the ASCII value of a character in java. This is done by using type-casting and normal variable assignment operations. 

Java Program to Find ASCII Value of a character

Java Program to Find ASCII Value of a character
//Java Program to Find ASCII Value of a Character

public class JavaPrograms {

	public static void main(String[] args) {

		char character = 't';
		
		//You can get by normal variable assignment
		int asciiValue = character;
		
		// You can also cast char to int
		int castAscii = (int) character;

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

	}
}
Output

The ASCII value of t is: 116

The ASCII value of t is: 116

Understand the program

In the above program, character t is stored in a char variable, character. Like, double quotes (" ") are used to declare strings, we use single quotes (' ') to declare characters.

Now, to get or find the ASCII value of a character, we just assign a character to an int variable asciiValue. Internally, Java converts the character value to an ASCII value.

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

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


Related Posts



Comments

Recent Posts
Tags