How to Print ASCII Value in Java17 Mar 2025 | 4 min read ASCII acronym for American Standard Code for Information Interchange. It is a 7-bit character set contains 128 (0 to 127) characters. It represents the numerical value of a character. For example, the ASCII value of A is 65. In this section, we will learn how to print ASCII value or code through a Java program. There are two ways to print ASCII value in Java:
Assigning a Variable to the int VariableTo print the ASCII value of a character, we need not use any method or class. Java internally converts the character value to an ASCII value. Let's find the ASCII value of a character through a Java program. In the following program, we have assigned two characters a and b in the ch1 and ch2 variables, respectively. To find the ASCII value of a and b, we have assigned ch1 and ch2 variables to the integer variables asciivalue1 and asciivalue2, respectively. Finally, we have printed the variable asciivalue1 and asciivalue2 in which ASCII values of the characters are stored. PrintAsciiValueExample1.java Output: The ASCII value of a is: 97 The ASCII value of b is: 98 Another way to write the above program is: PrintAsciiValueExample2.java Output: The ASCII value of a is: 97 The ASCII value of b is: 98 Similarly, we can print the ASCII value of other characters (A, B, C, …., Z) and symbols (!, @, $, *, etc.). Using Type-CastingType-casting is a way to cast a variable into another datatype. In the following program, we have declared two variables ch1 and ch2 of type char having the character a and b, respectively. In the next two lines, we have cast char type to int type using (int). After executing these two lines, the variable ch1 and ch2 are converted to an int variable ascii1 and ascii2, respectively. Finally, we have printed the variable ascii1 and ascii2 in which ASCII values of the characters are stored. PrintAsciiValueExample3.java Output: The ASCII value of a is: 97 The ASCII value of b is: 98 If we do not want to assign character, we can also take a character from the user. PrintAsciiValueExample4.java Output 1: Enter a character: P ASCII value of P is: 80 Output 2: Enter a character: G ASCII value of G is: 71 The following program prints the ASCII value (0 to 255) of all the characters. In the output, we have shown a few values. AsciiValueOfAllChracters.java Output: ![]() If we want to print the ASCII value of all the alphabets (A to Z), we can set the values in the loop and print them. AsciiValueAtoZ.java Output: ![]() Similarly, we can print the ASCII value of a to z by changing the loop in the above code. Next TopicJava Tutorial |
Java program to print the following pattern Algorithm: STEP 1: START STEP 2: SET lines=8 STEP 3: DEFINE i, j STEP 4: SET i=1.REPEAT STEP 5 to 14 UNTIL i STEP 5: SET j=1 STEP 6: REPEAT STEP 7 and 8 UNTIL j <=(lines/2) STEP 7: IF j is equals to...
3 min read
In this program, we need to convert the given binary tree to corresponding doubly liked list. The binary tree is a tree data structure in which each node has at most two children node. This can be achieved by traversing the tree in the in-order manner that...
6 min read
In this program, we create a doubly linked list and insert a new node in the middle of list. If list is empty, both head and tail will point to new node. If list is not empty, then we will calculate the size of the...
8 min read
Java Convert int to String We can convert int to String in java using String.valueOf() and Integer.toString() methods. Alternatively, we can use String.format() method, string concatenation operator etc. Scenario It is generally used if we have to display number in textfield because everything is displayed as a string in...
2 min read
In this program, we need to check whether all the leaves of the given binary tree are at same level or not. A Node is said to be leaf if it doesn't have any child node. In the below diagram, nodes 4, 5 and 6 are...
5 min read
In this section, we will learn how to reverse a number in Java using while loop, for loop and recursion. To reverse a number, follow the steps given below: First, we find the remainder of the given number by using the modulo (%) operator. Multiply the variable reverse...
5 min read
Java Convert int to char We can convert int to char in java using typecasting. To convert higher data type into lower, we need to perform typecasting. Here, the ASCII character of integer value will be stored in the char variable. To get the actual value in char...
2 min read
We can capitalize each word of a string by the help of split() and substring() methods. By the help of split("\\s") method, we can get all words in an array. To get the first character, we can use substring() or charAt() method. Let's see the example...
1 min read
In this program, we need to check whether the given matrix is an identity matrix. Identity Matrix A matrix is said to be an identity matrix if it is a square matrix in which elements of principle diagonal are ones, and the rest of the elements are...
4 min read
In this program, we need to find out the maximum width of the binary tree. The width of the binary tree is the number of nodes present in any level. So, the level which has the maximum number of nodes will be the maximum width...
6 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India