Write a java program to find ASCII value of a char || From A-Z || From a-z
char is a primitive data type in Java. Here in this tutorial, we will see What is the char in Java how can we find ASCII value of a char.
Char in Java
The Java char is a primitive data type. It is used to declare a type of character variables and a method. In Java Char can hold unsigned 16-bit Unicode characters with the range between 0 to 65,535 (inclusive).
Key Point in about Char in Java
- The char range lies between 0 to 65,535 (inclusive).
- Its default value of char in java is ‘\u0000’.
- char size in Java is 2 byte.
- It is used to store characters.
Find the ASCII value of a char in Java.
It’s really simple to find the ASCII value of a char in Java. We just need to convert the char into int. What is mean let’s try to understand by an example? We have a char ch = ‘A’. Here ch is a variable that holds a char ‘A’. Now if we change the type of char ‘A’ char to int then it will print an ASCII value of the char.
char ch = 'A'; int n = ch; System.out.println("Ascii Value of "+ch+" will be "+n);
Find ASCII value in Java from A-Z
In this example, we will find ASCII value-form A-Z. So to solve this problem the will follow two main step
- Convert the char into int.
- Run a loop from A-Z.
package com.demo; import java.util.*; class Main { public static void main(String[] args) { char CapitalChar = 'A'; char SmallChar = 'a'; //Find the ASCII Value from A-Z System.out.println("ASCII Value from 'A' to 'Z'"); for(int i ='A'; i<='Z'; i++) { System.out.println(CapitalChar +" : "+i); CapitalChar++; } } }
Find ASCII value in Java from a-z
System.out.println("ASCII Value from 'a' to 'z'"); //Find the ASCII Value from a-z for(int i ='a'; i<='z'; i++) { System.out.println(SmallChar +" : "+i); SmallChar++; }
Find ASCII value of a char in java
package com.demo; import java.util.*; class Main { public static void main(String[] args) { char Char= 'A'; System.out.println(Char+0); } }