Java Program to Find ASCII Value of a Character

In this program, you'll learn to find and display the ASCII value of a character in Java. This is done using type-casting and normal variable assignment operations.

Java Program to Find ASCII Value of a character

package com.javaguides.java.tutorial;

import java.util.Scanner;

/**
 * Java Program to Find ASCII Value of a character
 * 
 * @author https://www.sourcecodeexamples.net/
 *
 */
public class JavaProgram {
    public static void main(String[] args) {

        try (Scanner scanner = new Scanner(System.in)) {
            char ch = 'b';

            // ASCII value of char
            int ascii = ch;
            // You can also cast char to int
            int castAscii = (int) ch;

            System.out.println("The ASCII value of " + ch + " is: " + ascii);
            System.out.println("The ASCII value of " + ch + " is: " + castAscii);
        }
    }
}
Output:
The ASCII value of b is: 98
The ASCII value of b is: 98

Related Java Programs


Comments