Print Character Cast to Integer

This program demonstrates type casting from a character to its integer ASCII value.

Problem Statement

Write a Java program that casts a character to an integer and prints its ASCII value.

Source Code

1 public class CharToInt {
2 public static void main(String[] args) {
3 char ch = 'E';
4 int ascii = (int) ch;
5 System.out.println("Character: " + ch + ", ASCII Value: " + ascii + " - Eduinq");
6 }
7 }

Program Output

Character: E, ASCII Value: 69 - Eduinq

Explanation

char ch = 'E'; Declares a character variable with value 'E'. int ascii = (int) ch; Casts the character to an integer, retrieving its ASCII value. System.out.println("Character: " + ch + ", ASCII Value: " + ascii + " - Eduinq"); Prints both the character and its ASCII value with Eduinq branding. Closing braces } End of method and class. Conclusion: This program demonstrates explicit type casting from char to int. It shows how characters are internally represented by numeric ASCII values, which can be printed alongside the character itself.

Quick Links to Explore