The ASCII (American Standard Code for Information Interchange) is a 7-bit character set consisting of 128 characters ranging from 0 to 127. It assigns a numerical value to each character, representing its ASCII value. For example, the ASCII value of the character ‘A’ is 65.
In Java, there are two methods to print the ASCII value:
- Assigning a Variable to the int Variable
- Using Type-Casting
Assigning a Variable to the int Variable
In Java, the ASCII value of a character can be printed without using any specific method or class. Java automatically converts the character value to its corresponding ASCII value.
In the given program, the ASCII values of characters ‘a’ and ‘b’ are obtained by assigning them to integer variables asciivalue1 and asciivalue2, respectively. The ASCII values are then printed using System.out.println(). Here’s the rewritten content:
PrintAsciiValueExample1.java
public class PrintAsciiValueExample { public static void main(String[] args) { // character whose ASCII value to be found char ch1 = 'a'; char ch2 = 'b'; // variable that stores the integer value of the character int asciivalue1 = ch1; int asciivalue2 = ch2; System.out.println("The ASCII value of " + ch1 + " is: " + asciivalue1); System.out.println("The ASCII value of " + ch2 + " is: " + asciivalue2); } }
Output:
The ASCII value of a is: 97 The ASCII value of b is: 98
PrintAsciiValueExample2.java
public class PrintAsciiValueExample1 { public static void main(String[] String) { int ch1 = 'a'; int ch2 = 'b'; System.out.println("The ASCII value of a is: "+ch1); System.out.println("The ASCII value of b is: "+ch2); } }
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-Casting
Type-casting is a method used to convert a variable from one data type to another.
In the given program, variables ch1 and ch2 of type char are declared with the characters ‘a’ and ‘b’, respectively. The next two lines perform type-casting by converting the char type to int type using (int). This results in the conversion of ch1 and ch2 into int variables ascii1 and ascii2, respectively.
Finally, the values of ascii1 and ascii2 are printed, representing the ASCII values of the characters.
PrintAsciiValueExample3.java
public class PrintAsciiValueExample2 { public static void main(String[] args) { //characters whose ASCII value to be found char ch1 = 'a'; char ch2 = 'b'; //casting or converting a charter into int type int ascii1 = (int) ch1; int ascii2 = (int) ch2; System.out.println("The ASCII value of " + ch1 + " is: " + ascii1); System.out.println("The ASCII value of " + ch1 + " is: " + ascii2); } }
Output:
The ASCII value of a is: 97 The ASCII value of b is: 98
If we want to obtain a character from the user instead of assigning it directly.
PrintAsciiValueExample4.java
import java.util.Scanner; public class PrintAsciiValueExample3 { public static void main(String args[]) { System.out.print("Enter a character: "); Scanner sc = new Scanner(System.in); char chr = sc.next().charAt(0); int asciiValue = chr; System.out.println("ASCII value of " +chr+ " is: "+asciiValue); } }
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 of all characters (0 to 255). Here, we have shown a few sample values in the output.
AsciiValueOfAllChracters.java
public class AsciiValueOfAllChracters { public static void main(String[] args) { for(int i = 0; i <= 255; i++) { System.out.println(" The ASCII value of " + (char)i + " = " + i); } } }
Output:
The ASCII value of = 0 The ASCII value of = 1 The ASCII value of = 2 The ASCII value of = 3 The ASCII value of = 4 The ASCII value of = 5 The ASCII value of = 6 The ASCII value of = 7 The ASCII value of = 8 The ASCII value of = 9 The ASCII value of = 10 The ASCII value of = 11 The ASCII value of = 12 The ASCII value of = 13 The ASCII value of = 14 The ASCII value of = 15 The ASCII value of = 16 The ASCII value of = 17 The ASCII value of = 18 The ASCII value of = 19 The ASCII value of = 20 The ASCII value of = 21 The ASCII value of = 22 The ASCII value of = 23 The ASCII value of = 24 The ASCII value of = 25 The ASCII value of = 26 The ASCII value of = 27 The ASCII value of = 28 The ASCII value of = 29 The ASCII value of = 30 The ASCII value of = 31 The ASCII value of = 32 The ASCII value of ! = 33 The ASCII value of " = 34 The ASCII value of # = 35 The ASCII value of $ = 36 The ASCII value of % = 37 The ASCII value of & = 38 The ASCII value of ' = 39 The ASCII value of ( = 40 The ASCII value of ) = 41 The ASCII value of * = 42 The ASCII value of + = 43 The ASCII value of , = 44 The ASCII value of - = 45 The ASCII value of . = 46 The ASCII value of / = 47 The ASCII value of 0 = 48 The ASCII value of 1 = 49 The ASCII value of 2 = 50 The ASCII value of 3 = 51 The ASCII value of 4 = 52 The ASCII value of 5 = 53 The ASCII value of 6 = 54 The ASCII value of 7 = 55 The ASCII value of 8 = 56 The ASCII value of 9 = 57 The ASCII value of : = 58 The ASCII value of ; = 59 The ASCII value of < = 60 The ASCII value of = = 61 The ASCII value of > = 62 The ASCII value of ? = 63 The ASCII value of @ = 64 The ASCII value of A = 65 The ASCII value of B = 66 The ASCII value of C = 67 The ASCII value of D = 68 The ASCII value of E = 69 The ASCII value of F = 70 The ASCII value of G = 71 The ASCII value of H = 72 The ASCII value of I = 73 The ASCII value of J = 74 The ASCII value of K = 75 The ASCII value of L = 76 The ASCII value of M = 77 The ASCII value of N = 78 The ASCII value of O = 79 The ASCII value of P = 80 The ASCII value of Q = 81 The ASCII value of R = 82 The ASCII value of S = 83 The ASCII value of T = 84 The ASCII value of U = 85 The ASCII value of V = 86 The ASCII value of W = 87 The ASCII value of X = 88 The ASCII value of Y = 89 The ASCII value of Z = 90 The ASCII value of [ = 91 The ASCII value of \ = 92 The ASCII value of ] = 93 The ASCII value of ^ = 94 The ASCII value of _ = 95 The ASCII value of ` = 96 The ASCII value of a = 97 The ASCII value of b = 98 The ASCII value of c = 99 The ASCII value of d = 100 The ASCII value of e = 101 The ASCII value of f = 102 The ASCII value of g = 103 The ASCII value of h = 104 The ASCII value of i = 105 The ASCII value of j = 106 The ASCII value of k = 107 The ASCII value of l = 108 The ASCII value of m = 109 The ASCII value of n = 110 The ASCII value of o = 111 The ASCII value of p = 112 The ASCII value of q = 113 The ASCII value of r = 114 The ASCII value of s = 115 The ASCII value of t = 116 The ASCII value of u = 117 The ASCII value of v = 118 The ASCII value of w = 119 The ASCII value of x = 120 The ASCII value of y = 121 The ASCII value of z = 122 The ASCII value of { = 123 The ASCII value of | = 124 The ASCII value of } = 125 The ASCII value of ~ = 126 The ASCII value of = 127 The ASCII value of = 128 The ASCII value of = 129 The ASCII value of = 130 The ASCII value of = 131 The ASCII value of = 132 The ASCII value of = 133 The ASCII value of = 134 The ASCII value of = 135 The ASCII value of = 136 The ASCII value of = 137 The ASCII value of = 138 The ASCII value of = 139 The ASCII value of = 140 The ASCII value of = 141 The ASCII value of = 142 The ASCII value of = 143 The ASCII value of = 144 The ASCII value of = 145 The ASCII value of = 146 The ASCII value of = 147 The ASCII value of = 148 The ASCII value of = 149 The ASCII value of = 150 The ASCII value of = 151 The ASCII value of = 152 The ASCII value of = 153 The ASCII value of = 154 The ASCII value of = 155 The ASCII value of = 156 The ASCII value of = 157 The ASCII value of = 158 The ASCII value of = 159 The ASCII value of = 160 The ASCII value of ¡ = 161 The ASCII value of ¢ = 162 The ASCII value of £ = 163 The ASCII value of ¤ = 164 The ASCII value of ¥ = 165 The ASCII value of ¦ = 166 The ASCII value of § = 167 The ASCII value of ¨ = 168 The ASCII value of © = 169 The ASCII value of ª = 170 The ASCII value of « = 171 The ASCII value of ¬ = 172 The ASCII value of = 173 The ASCII value of ® = 174 The ASCII value of ¯ = 175 The ASCII value of ° = 176 The ASCII value of ± = 177 The ASCII value of ² = 178 The ASCII value of ³ = 179 The ASCII value of ´ = 180 The ASCII value of µ = 181 The ASCII value of ¶ = 182 The ASCII value of · = 183 The ASCII value of ¸ = 184 The ASCII value of ¹ = 185 The ASCII value of º = 186 The ASCII value of » = 187 The ASCII value of ¼ = 188 The ASCII value of ½ = 189 The ASCII value of ¾ = 190 The ASCII value of ¿ = 191 The ASCII value of À = 192 The ASCII value of Á = 193 The ASCII value of  = 194 The ASCII value of à = 195 The ASCII value of Ä = 196 The ASCII value of Å = 197 The ASCII value of Æ = 198 The ASCII value of Ç = 199 The ASCII value of È = 200 The ASCII value of É = 201 The ASCII value of Ê = 202 The ASCII value of Ë = 203 The ASCII value of Ì = 204 The ASCII value of Í = 205 The ASCII value of Î = 206 The ASCII value of Ï = 207 The ASCII value of Ð = 208 The ASCII value of Ñ = 209 The ASCII value of Ò = 210 The ASCII value of Ó = 211 The ASCII value of Ô = 212 The ASCII value of Õ = 213 The ASCII value of Ö = 214 The ASCII value of × = 215 The ASCII value of Ø = 216 The ASCII value of Ù = 217 The ASCII value of Ú = 218 The ASCII value of Û = 219 The ASCII value of Ü = 220 The ASCII value of Ý = 221 The ASCII value of Þ = 222 The ASCII value of ß = 223 The ASCII value of à = 224 The ASCII value of á = 225 The ASCII value of â = 226 The ASCII value of ã = 227 The ASCII value of ä = 228 The ASCII value of å = 229 The ASCII value of æ = 230 The ASCII value of ç = 231 The ASCII value of è = 232 The ASCII value of é = 233 The ASCII value of ê = 234 The ASCII value of ë = 235 The ASCII value of ì = 236 The ASCII value of í = 237 The ASCII value of î = 238 The ASCII value of ï = 239 The ASCII value of ð = 240 The ASCII value of ñ = 241 The ASCII value of ò = 242 The ASCII value of ó = 243 The ASCII value of ô = 244 The ASCII value of õ = 245 The ASCII value of ö = 246 The ASCII value of ÷ = 247 The ASCII value of ø = 248 The ASCII value of ù = 249 The ASCII value of ú = 250 The ASCII value of û = 251 The ASCII value of ü = 252 The ASCII value of ý = 253 The ASCII value of þ = 254 The ASCII value of ÿ = 255
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
public class AsciiValueAtoZ { public static void main(String[] args) { for(int i = 65; i <= 90; i++) { System.out.println(" The ASCII value of " + (char)i + " = " + i); } } }
Output:
The ASCII value of A = 65 The ASCII value of B = 66 The ASCII value of C = 67 The ASCII value of D = 68 The ASCII value of E = 69 The ASCII value of F = 70 The ASCII value of G = 71 The ASCII value of H = 72 The ASCII value of I = 73 The ASCII value of J = 74 The ASCII value of K = 75 The ASCII value of L = 76 The ASCII value of M = 77 The ASCII value of N = 78 The ASCII value of O = 79 The ASCII value of P = 80 The ASCII value of Q = 81 The ASCII value of R = 82 The ASCII value of S = 83 The ASCII value of T = 84 The ASCII value of U = 85 The ASCII value of V = 86 The ASCII value of W = 87 The ASCII value of X = 88 The ASCII value of Y = 89 The ASCII value of Z = 90
Similarly, we can print the ASCII value of lowercase alphabets (a to z) by modifying the loop in the above code.
for(int i = 97; i <= 122; i++)
We hope that this article has provided valuable insights into printing ASCII values in Java. For further knowledge, we recommend following tutorials.freshersnow.com.