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

A character variable ch is declared with the value 'E', and (int) ch casts it to an integer, retrieving its ASCII value, which is printed alongside the character, demonstrating explicit type casting from char to int.

Quick Links to Explore