Find the ASCII value of a character in Java

Understanding ASCII Values

ASCII (American Standard Code for Information Interchange) assigns numerical values to characters. For example, 'A' has an ASCII value of 65.

We will explore three different methods to find the ASCII value of a character in Java.

Method 1: Using Type Casting

This method prints the ASCII value of a character using type casting.

import java.util.Scanner;

public class ASCIIValue {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a character: ");
        char ch = scanner.next().charAt(0);
        System.out.println("ASCII value of " + ch + " is " + (int) ch);
        scanner.close();
    }
}
            
Input: A
Output: ASCII value of A is 65

Method 2: Using a Function

This method uses a function to return the ASCII value of a character.

import java.util.Scanner;

public class ASCIIValueFunction {
    public static int getASCII(char ch) {
        return (int) ch;
    }
    
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a character: ");
        char ch = scanner.next().charAt(0);
        System.out.println("ASCII value of " + ch + " is " + getASCII(ch));
        scanner.close();
    }
}
            
Input: z
Output: ASCII value of z is 122

Method 3: Using a Wrapper Class

This method uses the Character wrapper class to find the ASCII value.

import java.util.Scanner;

public class ASCIIValueWrapper {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a character: ");
        char ch = scanner.next().charAt(0);
        Integer asciiValue = Character.getNumericValue(ch) + 48;
        System.out.println("ASCII value of " + ch + " is " + asciiValue);
        scanner.close();
    }
}
            
Input: B
Output: ASCII value of B is 66
Strings

Below You will find some of the most important codes in languages like C, C++, Java, and Python. These codes are of prime importance for college semester exams and online tests.

Getting Started

Check whether a character is a vowel or consonant: C C++ Java Python

Check whether a character is an alphabet or not: C C++ Java Python

Find the ASCII value of a character: C C++ Java Python

Length of the string without using strlen() function: C C++ Java Python

Toggle each character in a string: C C++ Java Python

Count the number of vowels: C C++ Java Python

Remove the vowels from a string: C C++ Java Python

Check if the given string is Palindrome or not: C C++ Java Python

Print the given string in reverse order: C C++ Java Python

Remove all characters from string except alphabets: C C++ Java Python

Remove spaces from a string: C C++ Java Python

Replace a sub-string in a string: C C++ Java Python

Count common sub-sequences in two strings: C C++ Java Python

Compare two strings with wildcard support in one of them: C C++ Java Python

List all permutations of a given string in dictionary order: C C++ Java Python

Operations on Strings: C C++ Java Python