×

How to get ASCII value of char in Java

Introduction: On this application, you will learn how to find and show the ASCII value of char in Java. That is done with the use of type-casting and also everyday variable undertaking operations. To understand this situation, you ought to have an understanding of the subsequent Java programming subjects:

1. Java Date Type (Primitive)
2. Java Hello World Program

The full form of ASCII is American Standard Code Information Interchange. The ASCII code contain 7-bit characters. The ASCII code represent the numeric value of any given character. The 7-bit set contain 128 characters which is between 0 to 127.instance- The ASCII fee of H is seventy-two, ASCII value of B is 66. we can print the ASCII fee of man or woman A to Z, a to z, and special characters (@, #, $, ! and so on). To print the ASCII price, there are approaches.

To print the ASCII cost of a person, we need not apply any method or magnificence. Java programming language can convert the person's price internally to an ASCII value. allow observed the ASCII cost of a man or woman using the Java software program.

For print the ASCII value, there is two ways. These are-

  1. Assigning a Variable to the int Variable
  2. Using Type Casting

1. Assigning a Variable to the int Variable:

Now right here we speak approximately how we can assign a Variable to the int Variable. To print the ASCII price of a man or woman, we want no longer to use any method or magnificence. Java programming language can converts the individual fee internally to an ASCII cost. Permit found the ASCII fee of an individual the usage of the Java software.

In the below application, we have assigned characters a and b within the ch1 and ch2 variables, respectively. To find out the ASCII cost of a and b, we have set the ch1 and ch2 variables to the integer variables asciivalue1 and asciivalue2, respectively. ultimately, we've published the variables asciivalue1 and asciivalue2 in which ASCII values of the characters are stored.

Example 1 - Here we give an example of assign a Variable to the int Variable.

public class AsciiValue
{  
public static void main(String[] String)   
{  
//assigned two characters for found the ASCII value of those character 
int ch1 = 'A';  
int ch2 = 'B';  
//Print the result 
System.out.println("The ASCII value of A is: "+ch1);  
System.out.println("The ASCII value of B is: "+ch2);  
}  
}  

Output: For finding the result, we compile the above program and also run it.

The ASCII value of A is: 65
The ASCII value of B is: 66

Example 2 - Here we give an example of assign a Variable to the int Variable.

public class PrintAsciiValue  
{  
public static void main (String[] args)   
{  
//assigned two characters for found the ASCII value of those character  
char ch1 = 'H';  
char ch2 = 'k';  
// given variable stores the character’s integer value 
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: For finding the result, we compile the above program and also run it.

The ASCII value of H is: 72
The ASCII value of K is: 75

2. Using Type Casting:

Now here we discuss about the how we can using the Type Casting. It is a manner to convert a variable into some other data. In the following software, we have declared variables ch1 and ch2 of kind char having the man or woman cost of a and b respectively. within the subsequent two lines, we have forged char type to int kind the use of (int). In the end, we have revealed the variables ascii1 and ascii2 where ASCII values of the characters are saved.

Example 1 - Here we give an example of using the Type Casting.

import java.util.Scanner;  
public class AsciiValue  
{  
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: For finding the result, we compile the above program and also run it.

Enter a character: r
The ASCII value of r is: 82

Example 2 - Here we give an example of using the Type Casting.

public class PrintAsciiValue
{
public static void main(String [] args)
{
//assigned two characters for found the ASCII value of those character  
char ch = ‘f’;
int ascii = ch;
int castAscii = (int) ch;
//Print the result
System.out.println(“The ASCII value of ” + ch + " is: "+asciiValue);  
System.out.println(“The ASCII value of ” + ch + " is: "+ castAscii);  
}
}

Output: For finding the result, we compile the above program and also run it.

The ASCII value of f is: 70
The ASCII value of f is: 70

So, right here we speak approximately the way to get ASCII fee of char in Java. We also give some examples of it and also provide the output of the given program for better understand.


Related Topics

How to Create Immutable Classes in Java

Introduction Java is a programming language that is entirely object-oriented, and everything in it is seen as an object. And the blueprint or template of these objects are classes. Several classes...

3 minutes read.

Calculator Program in Java

Calculator Program in Java A calculator performs the basic mathematical operations such as addition, subtraction, multiplication, etc. In this section, we will create a calculator program in Java using switch case...

5 minutes read.

Memory Areas in Java

Let’s have a look at how memory management in Java works. We will be going to discuss how the objects get destroyed, the working of a garbage collector, and things...

5 minutes read.

Java Map Interface

A map is a collection that maps keys to values, with no duplicate keys allowed. The elements in a map are key/value pairs. HashMap: HashMap stores the keys in a...

3 minutes read.

Process and Thread in Java

Process It is a program that is running on your computer. The process is a heavy-weight, which takes memory separately to other processes. The process may be a small background task like spell-checker; it...

11 minutes read.

Java Integer floatValue() method

The floatValue() method of Integer class returns a float value for this Integer after a widening primitive conversion. Syntax public float floatValue() Parameters NA Specified by This method is specified by floatValue in class Number Return Value This...

1 minute read.

Monsoon Umbrella Problem in Java

The Monsoon Umbrella problem is a classic Java programming problem used to test the skills of a programmer. The problem involves writing a program to determine the number of umbrellas...

3 minutes read.

How to stop execution after a certain time in Java

We will learn how to stop a long-running execution after a set amount of time in this post. We will take a look at a few alternative approaches to this...

6 minutes read.

Access Modifier in Java

The access modifiers in java are used to change the accessibility and scope of a method, constructor, class, and fields. If you are aware of C++ language, when we declare any member...

2 minutes read.

String vs StringBuilder

String vs StringBuilder In this section, we will discuss the comparison between Java String and StringBuilder class. String In Java, a string is treated as an object that represents a sequence of characters....

4 minutes read.

Nth node from the end of the Linked list in Java

In talks with leading IT organizations like Google, Amazon, TCS, Accenture, etc., this extremely intriguing subject is constantly brought up. The goal of the problem-solving exercise is to evaluate the...

6 minutes read.

Difference between = = and equals ( ) in java

Java : Java is a pure object oriented language. It was introduced by James Gosling in the year 1995. The first public implementation of java was done by sun micro systems...

6 minutes read.

Sierpinski Number in Java

The Sierpinski triangle—is it a fractal? The Sierpinski Triangle fractals. A self-similar fractal is the Sierpinski triangle. It is made of an equilateral triangle with its residual area successively reduced by...

3 minutes read.

Segment Tree in Java

Binary trees can address a variety of issues; however, the Segment Tree is more efficient in terms of time complexity. The segment tree in Java is represented using an array. Native...

4 minutes read.

Implementing Queue Using Array in Java

We can implement queue by using array in Java. The queue is a linear data structure, and the array is one of the simplest data structures. Before implementing a queue...

4 minutes read.

Java Return Keyword

The return keyword in Java is used to end a method's execution. the caller receives the return, followed by the appropriate value. The return type of the method, such as...

3 minutes read.

Rotate matrix by 90 degrees in Java | Rotate matrix in Java clockwise and anti-clockwise

In this article, you will be acknowledged about what is a matrix along with an example. Most importantly you will be equipped knowledge on how to rotate the matrix by...

4 minutes read.

Java OCR

In this article, you will be acknowledged about what is a tesseract OCR, how it works, what are its used and advantages and disadvantages. Also, you will be able to...

4 minutes read.

Advanced Java skills

These were some of the contemporary talents that a Java developer should master in efforts to progress in the era of science in 2022. A database such as MySQL, a...

4 minutes read.

Tribonacci series in Java

The Fibonacci series and the Tribonacci sequence are linked. The Fibonacci sequence, each element summates the three preceding terms, is expanded into the Tribonacci series in Java. Through specific examples,...

3 minutes read.