Cast Char to Int

This program demonstrates casting a char to int.

Problem Statement

Write a Java program that casts a char to int and prints ASCII value.

Source Code

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

Program Output

Character: Z, ASCII: 90 - Eduinq

Explanation

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

Quick Links to Explore