In this Java tutorial, you’ll learn how to find the ascii value of a character using Java programming language.
ASCII Value (American Standard Code for Information Interchange) is a character that encoded standard for text files in computers.
How to Find the ASCII value of a Character in Java?
In this program, we’ll learn how to print ASCII values or code through JAVA program.
RUN CODE SNIPPETExample 1:
public class Main { public static void main(String[] args) { char cha1 = 'a'; char cha2 = 'S'; char cha3 ='%'; int asciivalue1 = cha1; int asciivalue2 = cha2; int asciivalue3 = cha3; System.out.println("The ASCII value of " + cha1 + " is: " + asciivalue1); System.out.println("The ASCII value of " + cha2 + " is: " + asciivalue2); System.out.println("The ASCII value of " + cha3 + " is: " + asciivalue3); } }
Output
The ASCII value of a is: 97 The ASCII value of S is: 83 The ASCII value of % is: 37
In the above program, we have declared three variables cha1, cha2, cha3 of type characters having the characters a, S and % respectively.
To convert the variable into an int variable we cast char type to int type.
Print the ASCII value using println function.
RUN CODE SNIPPETExample 2: ASCII value from A TO ZExample 2: ASCII value from A TO
public class Main { 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
Using the loop statement, we can print all alphabets value from A to Z.