Java Program to Print the ASCII Value
ASCII is an acronym that stands for American Standard Code for Information Interchange. In ASCII, a specific numerical value is given to different characters and symbols, for computers to store and manipulate, and while storing and manipulating the electronic device always works with the binary value of the ASCII number given. As it is impossible to do that in the original form.
Approaches: There are 4 ways to print ASCII value or code of a specific character which are listed below briefing the concept followed by a java example for the implementation part.
- Using brute force Method
- Using the type-casting Method
- Using the format specifier Method
- Using Byte class Method
Method 1: Assigning a Variable to the int Variable
In order to find the ASCII value of a character, simply assign the character to a new variable of integer type. Java automatically stores the ASCII value of that character inside the new variable.
Implementation: Brute force Method
The ASCII value of } is: 125
Method 2: Using Type-Casting
Type-casting in java is a way to cast a variable into another datatype which means holding a value of another datatype occupying lesser bytes. In this approach, a character is a typecast of type char to the type int while printing, and it will print the ASCII value of the character.
The ASCII value of } is: 125
Note: In above method 1 and method 2, both the methods are one type of typecasting. In method 1, typecasting is done automatically by the compiler. In method 2, typecasting it manually so the method 2 is much more efficient than method 1 as the compiler has to put lesser effort. Also, remember typecasting done automatically is called implicit typecasting and where it is done from the user end is called explicit typecasting
Method 3: Using format specifier (More Optimal)
In this approach, we generate the ASCII value of the given character with the help of a format specifier. We have stored the value of the given character inside a formal specifier by specifying the character to be an int. Hence, the ASCII value of that character is stored inside the format specifier.
The ASCII value of the character ' } ' is 125
Method 4: Finding the ASCII value by generating byte (Most Optimal)
- Initializing the character as a string.
- Creating an array of type byte by using getBytes() method.
- Printing the element at ‘0’th index of the bytes array.
This is the ASCII value of our character residing at the ‘0’th index of the string. This method is generally used to convert a whole string to their ASCII values. For the characters violating the encoding exception, the try-catch is given.
The ASCII value of } is 125
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.