ASCII to Character in Java

Convert an integer ASCII code to its corresponding character.

BeginnerModule 1: Basic Java ProgramsExample 17 of 20
ascii-to-character.java
1import java.util.Scanner;
2
3public class Main {
4 public static void main(String[] args) {
5 Scanner sc = new Scanner(System.in);
6
7 System.out.print("Enter ASCII value (0-127): ");
8 int code = sc.nextInt();
9
10 char ch = (char) code;
11 System.out.println("Character for ASCII " + code + " = " + ch);
12
13 sc.close();
14 }
15}

Output

Enter ASCII value (0-127): 65
Character for ASCII 65 = A

What's going on

We cast the integer value to char to obtain the corresponding character.