Using Type Casting

Typecasting is the process of forcefully converting a value of one data type into a value of another data type. This is usually done with respect to the ASCII table, a character encoding standard for electronic communication. Every alphabet or symbol that can be entered digitally has a corresponding unique integer number. For example, the alphabet ‘A’ has an ASCII code of 65. By typecasting, we’re forcefully converting the character value into its equivalent ASCII integer code. Here is our Java char to int example using type casting.

Java char to int example (using type Casting)

import java.util.Scanner;
public class CharToInt {

    public static void main(String[] args) {

        //Taking Input from User and storing into char value
        Scanner input = new Scanner(System.in);
        System.out.print("Input any character: ");
        char c = input.next().charAt(0);

        //Typecasting the value into an integer variable
        int a = (int) c;
        System.out.print("The given character in integer is " + a);

    }
}
The output is:
Input any character: q The given character in integer is 113

Using getNumericValue()

getNumericValue() works similarly to typecasting, but instead of following the ASCII table, it follows the Unicode encoding standard. ASCII can only represent a specific set of symbols and characters in the English language, Unicode contains over a million values containing letters and symbols for every language in the world. For example, the value of A in Unicode is \u0041 which is equivalent to a numeric value of 10. This method works normally for numeric values stored in a char variable by converting a character to an integer data type.
import java.util.Scanner;
public class CharToInt {

    public static void main(String[] args) {

        //Taking Input from User and storing into char value
        Scanner input = new Scanner(System.in);
        System.out.print("Input any character: ");
        char c = input.next().charAt(0);

        //Using getNumericValue() to convert into an Integer Value
        int a=Character.getNumericValue(c);
        System.out.print("The given character in integer is " + a);

    }
}
The output is:
Input any character: q The given character in integer is 26

Using ParseInt()

ParseInt() is another intuitive method to convert the char to int. It's also widely used to cross convert between other numeric data types as well such as a float, double, and long. ParseInt() requires at least one argument and at the most, two arguments. The first argument is the variable name of the value we’re converting. The second argument is the radix which refers to the base value of decimal, octal, hexadecimal, etc. Due to the ability of ParseInt() to convert data types with the number system in mind as well, it's an optimal choice for converting “numerical” char to int in java. However it won’t be able to convert the alphabets. It can only convert char to int in java when there are numeric values stored in the char data type.
import java.util.Scanner;
public class CharToInt {

    public static void main(String[] args) {

        //Taking Input from User and storing into char value
        Scanner input = new Scanner(System.in);
        System.out.print("Input any character: ");
        char c = input.next().charAt(0);

        //Using ParseInt to convert into an Integer Value
        int a=Integer.parseInt(String.valueOf(c));
        System.out.print("The given character in integer is " + a);

    }
}
Java Convert Char to Int With Examples - 2