Open In App

Java Program to Convert Char to Int

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Given a character in Java, your task is to write a Java program to convert this given character into an integer. In Java, we can convert the Char to Int using different approaches. If we direct assign a char variable to int, it will return the ASCII value of a given character. If the char variable contains an int value, we can get the int value by calling the Character.getNumericValue(char) method. Alternatively, we can use String.valueOf(char) method.

Examples of Conversion from Char to Int

Input : ch = ‘3’
Output : 3

Input : ch = ‘9’
Output : 9

Integer: The Integer or int data type is a 32-bit signed two’s complement integer. Its value range lies between – 2,147,483,648 (-2^31) to 2,147,483,647 (2^31 -1) (inclusive). Its minimum value is – 2,147,483,648 and its maximum value is 2,147,483,647. Its default value is 0. The int data type is generally used as a default data type for integral values unless there is no problem with memory.

Example:

int a = 10

Character: The char data type is a single 16-bit Unicode character. Its value range lies between ‘\u0000’ (or 0) to ‘\uffff’ (or 65,535 inclusive). The char data type is used to store characters.

Example:

char ch = 'c'

Methods to Convert Char to Int in Java

There are numerous approaches to the conversion of Char datatype to Integer (int) datatype. A few of them are listed below.

  • Using ASCII Values
  • Using String.valueOf() Method
  • Using Character.getNumericValue() Method

1. Using ASCII values for Char to Int Conversion 

This method uses TypeCasting to get the ASCII value of the given character. The respective integer is calculated from this ASCII value by subtracting it from the ASCII value of 0. In other words, this method converts the char to int by finding the difference between the ASCII value of this char and the ASCII value of 0.

Example:

Java




// Java Program to Convert Char to Int
// Using ASCII value
 
// Class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Initializing a character
        char ch = '3';
 
        // Printing the character value
        System.out.println("char value: " + ch);
 
        // Converting character to its integer value
        int a = ch - '0';
 
        // Printing the integer value
        System.out.println("int value: " + a);
    }
}


Output

char value: 3
int value: 3

2. Using the String.valueOf() method for Char to Int Conversion

The method valueOf() of class String can convert various types of values to a String value. It can convert int, char, long, boolean, float, double, object, and char array to String, which can be converted to an int value by using the Integer.parseInt() method.  The below program illustrates the use of the valueOf() method.

Example: 

Java




// Java program to convert Char to Int
// Using valueOf() method of String Class
 
// Class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Declaring and initializing a character
        char ch = '3';
 
        // Printing the character value
        System.out.println("char value: " + ch);
 
        // Converting the character to it's integer value
        // using valueOf() method
        int a = Integer.parseInt(String.valueOf(ch));
 
        // Printing the integral value
        // corresponding to its character value
        System.out.println("int value: " + a);
    }
}


Output

char value: 3
int value: 3

3. Using getNumericValue() method of Character Class 

The getNumericValue() method of class Character is used to get the integer value of any specific character. For example, the character ‘9’ will return an int having a value of 9. The below program illustrates the use of the getNumericValue() method.

Example: 

Java




// Java Program to Convert Character to Integer
// Using getNumericValue() method of Character Class
 
// Driver Class
class GFG {
    // Main driver method
    public static void main(String[] args)
    {
        // Declaring and initializing a character
        char ch = '3';
 
        // Displaying above character on console
        System.out.println("char value: " + ch);
 
        // Converting the Character to it's int value
        // using getNumericValue() method of Character Class
        int a = Character.getNumericValue(ch);
 
        // Printing the corresponding integral value
        System.out.println("int value: " + a);
    }
}


Output

char value: 3
int value: 3



Last Updated : 20 Aug, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads