How to Convert char to int in Java (Beginner’s Guide)

Convert char to int in Java with Examples

  • Last Updated : 16 Dec, 2019

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_noneeditplay_arrowbrightness_4// 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); } } 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_noneeditplay_arrowbrightness_4// 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); } } 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_noneeditplay_arrowbrightness_4// 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); } } Output:char value: 3 int value: 3

Java Convert char to int

We can convert char to int in java using various ways. If we direct assign char variable to int, it will return ASCII value of given character.

Java Convert char to int

If char variable contains int value, we can get the int value by calling Character.getNumericValue(char) method. Alternatively, we can use String.valueOf(char) method.

1) Java char to int Example: Get ASCII value

Let’s see the simple code to convert char to int in java.

  1. public class CharToIntExample1{  
  2. public static void main(String args[]){  
  3. char c=’a’;  
  4. char c2=’1′;  
  5. int a=c;  
  6. int b=c2;  
  7. System.out.println(a);  
  8. System.out.println(b);  
  9. }}  

Output:

97
49

2) Java char to int Example: Character.getNumericValue()

Let’s see the simple code to convert char to int in java using Character.getNumericValue(char) method which returns an integer value.

  1. public class CharToIntExample2{  
  2. public static void main(String args[]){  
  3. char c=’1′;  
  4. int a=Character.getNumericValue(c);  
  5. System.out.println(a);  
  6. }}  

Output:

1

3) Java char to int Example: String.valueOf()

Let’s see another example which returns integer value of specified char value using String.valueOf(char) method.

  1. public class CharToIntExample3{  
  2. public static void main(String args[]){  
  3. char c=’1′;  
  4. int a=Integer.parseInt(String.valueOf(c));  
  5. System.out.println(a);  
  6. }}  

Output:

1

Leave a Reply

Your email address will not be published. Required fields are marked *

Psst. Yeah, you. Want some marketing secrets "gurus" never share?

Get them every month from your favorite teen entrepreneur - for free. After all, sharing is caring. ❤️🚀

🔒 No spam. I promise. 🔒