How to get ascii value of char in java: If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list available.
Find ASCII Value of a character
Java get ascii value of char: In this article we will see different ways to find ASCII value of a character. Before that first we will understand what is this ASCII code and then we will see how to get that.
ASCII Code :
Get ascii value of char java: ASCII stands for American Standard Code for Information Interchange. It is a unique code which is used for electronic communication. With the help of this encryption standard characters and symbols are represented with unique numbers which all are having its binary format and computer system understand 0s and 1s. Actually, Java uses Unicode which includes ASCII and other characters from world.
ASCII value of small letters i.e a = 97, b = 98, c = 99 ............... x = 120, y = 121, z = 122 ASCII value of capital letters i.e A = 65, B = 66, C = 67 .............. X = 88, Y = 89, Z = 90
Different ways to get ASCII value of characters :
Method#1 – Assigning char variable to the int Variable :
Java get ascii value: When we will assign a character variable to an integer variable then internally character value will be converted into ASCII value.
Approach :
- Take a character input from user.
- Create an integer variable.
- Then assign that character variable to integer variable.
- Then that char value will be converted into int value representing ASCII code of the character.
Let’s see the below program.
// imported java util package // because Scanner class present in java util package import java.util.*; public class ASCII { // main method public static void main(String[] args) { // Scanner class object created to take input. Scanner sc=new Scanner(System.in); System.out.print("Enter a character: "); // Taking character input from user // And assigning it to character variable 'letter char letter = sc.next().charAt(0); // assigning character variable 'letter' to int variable 'code' int code = letter; System.out.println("ASCII value of " + letter + " is: " + code); } }
Output: Enter a character: d ASCII value of d is: 100 //Another case Enter a character: Z ASCII value of Z is: 90
Method#2 – Implementing Type casting :
Java get character ascii value: By the help of type casting also we can get the ASCII value of a character.
Actually Type casting is converting value of one data type to another data type.
syntax: (datatype_to be converted_into) variable_to_be_converted;
Where,
- datatype_to be converted_into : Represents the data type to which the respective variable will be changed.
- variable_to_be_converted : Represents the variable that needs to be converted into another data type.
Approach :
- Take a character user input.
- Create an integer variable.
- Then assign that character variable to integer variable along with casting.
- Then that char value will be converted into int value representing ASCII code of the character.
Let’s see the below program.
// imported java util package // because Scanner class present in java util package import java.util.*; public class ASCII { // main method public static void main(String[] args) { // Scanner class object created to take input. Scanner sc=new Scanner(System.in); System.out.print("Enter a character: "); // Taking character input from user // And assigning it to character variable 'letter char letter = sc.next().charAt(0); // character variable type casted into int value int code = (int) letter; System.out.println("ASCII value of " + letter + " is: " + code); } }
Output: Enter a character: K ASCII value of K is: 75 //Another case Enter a character: n ASCII value of n is: 110
Are you seeking professional help for coding in the Java programming language? The tutorial of Java Programming Examples for beginners and experts will strongly improve your coding skills then you can program for any logic in Java.
Related Java Basic Programs:
- Java Program to Print an Integer (Entered by the User)
- Java Program to Add Two Integers
- Java Program to Multiply two Floating Point Numbers
- Java Program to Compute Quotient and Remainder
- Java Program to Swap Two Numbers
- Java Program to Check Whether a Number is Even or Odd
- Java Program to Find the Largest Among Three Numbers
- Java Program to Find the Frequency of Character in a String
- Java Program to Remove All Whitespaces from a String
- Java Program to Round a Number to n Decimal Places
- Java Program to Check if a String is Empty or Null