Table of contents
1.
Introduction
2.
How to Convert Character to Integer in Java?
3.
1. Using ASCII Method for Char to Int Conversion 
4.
2. Using String.valueOf() method for Char to Int Conversion 
5.
3. Using Character.getNumericValue() method for Char to Int Conversion 
6.
Frequently Asked Questions
6.1.
What is the difference between a char and an int in Java?
6.2.
Can I use Integer.parseInt() to convert a char to an int?
6.3.
In the typecasting method, how do we convert the given character to int?
6.4.
What is the function of the valueOf() method of class String?
7.
Conclusion
Last Updated: Dec 6, 2024
Easy

Char to Int in Java

Author Muskan Gupta
4 upvotes
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Java has two types of data types. They are primitive data types and non-primitive data types. int, char, long, float, etc., belong to primitive data types. In some cases, it is required to perform operations on numeric values, where variable values are specified in the char data type. In such cases, these character values are converted to numeric values, or we can say the conversion of char to int in Java happens, and then calculations are performed. In this blog, we will learn different approaches to how to convert char to int in Java.

Char to Int in Java

How to Convert Character to Integer in Java?

There are several methods available for converting data from the Char type to the Integer (int) type. Here are a few of them:

  1. Using ASCII values
  2. Using String.valueOf() Method 
  3. Using .getNumericValue() Method

So, let us start our journey to learn how to convert char to int in Java.

1. Using ASCII Method for Char to Int Conversion 

In this method, we use type casting to get the ASCII value of the target character. The corresponding int value to that particular character is calculated by subtracting the ASCII value of a given character from the ASCII value of 0.

In short, we can say that this method converts the char to int in java by calculating the difference between the ASCII values of the given char and 0.

Example 1 

// Java program to convert char to int in java using ASCII value

class CN {
	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);
	}
}
You can also try this code with Online Java Compiler
Run Code

Output

char value: 3
int value: 3

Here, ch='3'. When two characters get subtracted, their ASCII values get subtracted. Since the ASCII values of '3' and '0' are 51 and 48, respectively. So, the resulting difference variable a is assigned 51-48=3.

Example 2 

public class CharToIntExample2{  
	public static void main(String args[]) {  
		char c = 'a';  
		char c2 = '1';  
		int a = c;  
		int b = c2;  
		System.out.println(a);  
		System.out.println(b);  
	}
} 
You can also try this code with Online Java Compiler
Run Code

Output

97
49

Here, c='a' and c2='1'. When the character variable is assigned to an integer variable, the integer variable is assigned the ASCII value of the character variable. Since the ASCII values of 'a' and '1' are 97 and 49, respectively. So, the printed values are 97 and 49.

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

In class String, a method valueOf() is used to convert several types of values to a String value. We can use it to convert int, char, long, boolean, float, double, object, and char array to String.

This String can be converted to an int value by using the method Integer.parseInt(). 

Example

Here is a program that shows the use of the valueOf() method.

// Java program to convert char to int in java using String.valueOf()

class CN {
	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);
	}
}
You can also try this code with Online Java Compiler
Run Code

Output

char value : 3
int value : 3

Here, String.valueOf(ch) is converting the character to string, and Integer.parseInt is converting that string into int.                                

3. Using Character.getNumericValue() method for Char to Int Conversion 

The method getNumericValue() of class Character can be used to get the integer value of any specific character

For example, the character '5' would return an int having a value of 5. And, the character 'a' would return int with a value of 97(a's ASCII value).

Example

We will see a program written below that illustrates the getNumericValue() method.

// Java program to convert char to int in java using Character.getNumericValue()

class CN {
public static void main(String[] args) {
	// Initializing a character(ch)
	char ch = '5';
	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);
	}
}
You can also try this code with Online Java Compiler
Run Code

Output

char value: 5
int value: 5

Here,  getNumericValue() is used for converting the character to integer.

Frequently Asked Questions

What is the difference between a char and an int in Java?

In Java, char represents a single 16-bit Unicode character, while int represents a 32-bit integer. char stores characters, while int stores numeric values.

Can I use Integer.parseInt() to convert a char to an int?

No, Integer.parseInt() cannot directly convert a char to an int. To convert, you can use (int) casting or Character.getNumericValue(char) to obtain the integer value of a char.

In the typecasting method, how do we convert the given character to int?

This method converts the given character to int by finding the difference between the ASCII value of this char and the ASCII value of 0.

What is the function of the valueOf() method of class String?

The function of the valueOf() method of class String is to convert various data types like int, char, boolean, float, double, and char array to a String Value.

Conclusion

Converting a char to an int in Java is a straightforward process but requires understanding the underlying difference between character representation and numeric values. By using explicit casting, Character.getNumericValue(), or through arithmetic operations, developers can effectively convert and manipulate char values as integers.

Recommended Readings:

Live masterclass