Convert char to int in Java with Examples

Given a character in Java, the task is to convert this character into an integer.

Input: ch = '3'
Output: 3

Input: ch = '9'
Output: 9

There are various ways in which one can convert char to its int value.

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

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to convert
    // char to int using ASCII value
      
    class GFG {
        public static void main(String[] args)
        {
      
            // Initializing a character(ch)
            char ch = '3';
            System.out.println("char value: " + ch);
      
            // Converting ch to it's int value
            int a = ch - '0';
            System.out.println("int value: " + a);
        }
    }

    chevron_right

    
    

    Output:



    char value: 3
    int value: 3
    
  2. Using String.valueOf(): The method valueOf() of class String, can be used to convert various types of values to String value, It can be used to convert int, char, long, boolean, float, double, object and char array to String. which further can be converted to an int value by using Integer.parseInt() method.

    Below program illustrates the use of the valueOf() method.

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to convert
    // char to int using String.valueOf()
      
    class GFG {
        public static void main(String[] args)
        {
            // Initializing a character(ch)
            char ch = '3';
            System.out.println("char value: " + ch);
      
            // Converting the character to it's int value
            int a = Integer.parseInt(String.valueOf(ch));
            System.out.println("int value: " + a);
        }
    }

    chevron_right

    
    

    Output:

    char value: 3
    int value: 3
    
  3. Using Character.getNumericValue(): 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 value 9.

    Below program illustrates the use of getNumericValue() method.

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to convert char to int
    // using Character.getNumericValue()
      
    class GFG {
        public static void main(String[] args)
        {
      
            // Initializing a character(ch)
            char ch = '3';
            System.out.println("char value: " + ch);
      
            // Converting the Character to it's int value
            int a = Character.getNumericValue(ch);
            System.out.println("int value: " + a);
        }
    }

    chevron_right

    
    

    Output:

    char value: 3
    int value: 3
    



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.