Java

Java Convert Char to Int With Examples

While programming in Java, there can be instances where the programmer needs to use a character value(s) differently. For instance, fetching the corresponding “hexadecimal” or “ASCII” representation against the character(s). In such situations, converting “char” to “int” in Java does wonders in streamlining the conversion techniques at the developer’s end.

This blog will discuss the approaches to converting “char” to “int” in Java.

How to Convert/Transform “Char” to “Int” in Java With Examples?

To transform “char” to “int” in Java, apply the following approaches:

Approach 1: Convert Char to Int in Java Using “Character.getNumericValue()” Method

The “Character.getNumericValue()” method returns the int value of the specified character, and in the case of the character not having any int value, “-1” is returned. This method can be applied to convert the specified character into an integer simply.

Syntax

getNumericValue(x)

In the above syntax, “x” corresponds to the character that needs to be converted into an integer.

Example

Let’s overview the following example:

char character= 'A';

int assign = Character.getNumericValue(character);

System.out.println("The integer is: "+assign);

In the above lines of code:

  • First, initialize the character.
  • In the next step, apply the “Character.getNumericValue()” method, accumulating the specified character as its parameter.
  • Lastly, transform the initialized character as the method’s parameter into an “integer”.

Output

In the above output, it can be seen that the corresponding integer is returned, which is also a “hexadecimal” representation of the character.

Approach 2: Convert Char to Int(ASCII Representation) in Java By Assigning “int” Data Type

In this approach, the “char” can be converted into an integer, i.e., “ASCII” representation, by assigning the “int” data type to the initialized integer:

char character= 'A';

int assign= character;

System.out.println("The ASCII value is: "+assign);

In the above code block:

  • Likewise, initialize the character that needs to be converted into an integer.
  • Then, allocate the “int” data type to the character.
  • Finally, display the corresponding integer equivalent to its “ASCII” representation.

Output

In this output, it can be analyzed that the corresponding integer, i.e., “ASCII” equivalent, is returned.

Approach 3: Convert Char to Int in Java Using “parseInt()” and “String.valueOf()” Methods

The “parseInt()” method is used to get the primitive data type of the “String”, and the “String.valueOf()” method transforms different types of values into a string. These methods can be combined to convert the character into a string and then return an integer representation of the string.

Syntax

parseInt(x, y)

In this syntax:

  • x” refers to the string representation of decimal.
  • y” converts “x” into an integer.
String valueOf(character)

In the above-given syntax, “character” corresponds to the character that needs to be transformed into a string.

Example

Let’s go through the below-given code lines:

char character= '1';

int assign = Integer.parseInt(String.valueOf(character));

System.out.println("The integer is: "+assign);

According to the above code, apply the following steps:

  • Similarly, initialize the character to be transformed into an integer.
  • Now, apply the combined “parseInt()” and “String.valueOf()” methods first to convert the initialized character into a string and then transform the string into an integer.
  • Lastly, log the transformed “integer” representation of the character.

Output

This output implies that the desired requirement is fulfilled.

Conclusion

To convert char to int in Java, apply the “Character.getNumericValue()” method, assign “int” Data Type, or use the combined “parseInt()” and “String.valueOf()” methods. These approaches return the “int” value directly, by assigning the required data type or by transforming the character into a string first and parsing it into an integer, respectively. This blog elaborated on the approaches to converting “char” to “int” in Java.

About the author

Umar Hassan

I am a Front-End Web Developer. Being a technical author, I try to learn new things and adapt with them every day. I am passionate to write about evolving software tools and technologies and make it understandable for the end-user.