How to Convert Char to Int in Java?


In Java, smaller datatype can be converted to bigger datatype. Here, we will see how to convert char datatype to int datatype. The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive). The int data type is a 32-bit signed two's complement integer, which has a minimum value of -231 and a maximum value of 231-1.

Let's deep dive into this article, to know how it can be done by using Java programming language.

For instance

Suppose the given char datatype is '9'.

Then it will be converted to int datatype and result will be:

After conversion of char to int: 9

Algorithm

Step-1: Declare the char datatype.

Step-2: Convert it into int datatype.

Step-3: Print the result.

Syntax

valueOf() Method: In java, valueOf() method converts different types of values like float, byte, int etc into string..

getNumericValue() Method: In java, the getNumericValue() method of character class returns the int value of the specified character. It belongs to java.lang.Character package.

Multiple Approaches

We have provided the solution in different approaches.

  • By Using ASCII Values

  • By Using String.valueOf() Method

  • By Using Character.getNumericValue() Method

Let's see the program along with its output one by one.

Approach-1: By Using ASCII Values

In this approach, char is converted to int by subtracting ASCII value of ‘0' to ASCII value by given char.

Example

public class Main {
	// Main method
	public static void main(String[] args)
	{
		//Declaring a character
		char ch = '9';
		//Converting char to int
		int a = ch - '0';
		//Printing the int
		System.out.println("After conversion of char to int: " + a);
	}
}

Output

After conversion of char to int: 9

Approach-2: By Using valueOf() Method

In this approach, valueOf() method is used. valueOf() method converts char type of value to a String value which can be converted to an int value by using the Integer.parseInt() method.

Example

public class Main {
	// Main method
	public static void main(String[] args)
	{
		//Declaring a character
		char ch = '1';
		//Conversion of char to int
		int a = Integer.parseInt(String.valueOf(ch));

		//printing the int value
		System.out.println("After conversion of char to int: " + a);
	}
}

Output

After conversion of char to int: 1

Approach-3: By Using getNumericValue() Method

In this approach, we take date input from the system. Then we use the Calendar class to obtain Date from timestamp.

In this approach, getNumericValue() will be used. It is a method of class Character and is used to get the integer value of any specific character.

Example

public class Main {
	// Main method
	public static void main(String[] args)
	{
		//Declaring a character
		char ch = '6';
		//Converting char to int
		int a = Character.getNumericValue(ch);
		// Printing the int value
		System.out.println("int value: " + a);
	}
}

Output

int value: 6

In this article, we explored different approaches to convert Char to Int by using Java programming language.

Updated on: 17-Aug-2023

307 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements