Convert a char to int Java


JavaViews 3834

This tutorial will guide you through different methods to convert a char to an int in Java using various examples. A char is a data type to store a single character and an int is a data type to store a numeric value.

Convert char to int in Java

Different methods for char to int conversion in Java

There are different ways to convert a char to int in Java.

Convert char to int in Java

Implicit type casting

Normally, whenever we want to convert a char variable to an int data type, our main is aim to retrieve its equivalent ASCII value. We can achieve this implicitly by assigning the char variable directly to an int variable. The compiler implicitly and automatically converts the character to its equivalent ASCII value which is an integer. We call this process type promotion or implicit typecasting.

public class ConvertCharToInt {

  public static void main(String[] args) {
    char c = 'a';
    char v = 'b';
    int i = c;
    int j = v;
    
    System.out.println("ASCII value of '" + c + "' is: " + i);
    System.out.println("ASCII value of '" + v + "' is: " + j);

  }

}
ASCII value of 'a' is: 97
ASCII value of 'b' is: 98

Explicit typecasting

We can explicitly typecast a char variable to an integer by converting it to an int type. In the below example, we can see that we explicitly convert the char variable by typecasting it to int data type. For this, we mention (int) with in paranthesis. This process is called as explicit type casting.

public class ConvertCharToInt {

  public static void main(String[] args) {
    char c = 'a';
    char v = 'b';
    
    
    System.out.println("ASCII value of '" + c + "' is: " + (int)c);
    System.out.println("ASCII value of '" + v + "' is: " + (int)v);

  }

}
ASCII value of 'a' is: 97
ASCII value of 'b' is: 98

Convert char to int using Character.getNumericValue

The Character class contains a method getNumericValue() that converts a character to an integer value. In the below example, the getNumericValue method converts the character value to its equivalent integer value.

public class ConvertCharToInt {

  public static void main(String[] args) {
    char c = '2';
    char v = '3';
    
    int i = Character.getNumericValue(c);
    int j = Character.getNumericValue(v);
    
    System.out.println("The integer value of '" + c + "' is: " + i);
    System.out.println("The integer value of '" + v + "' is: " + j);

  }

}
The integer value of '2' is: 2
The integer value of '3' is: 3

Convert char to int using String.valueOf

Another way to convert a char to int using the valueOf() method of the String class. Using this value, we can then convert to an integer using the parseInt() method of the Integer class.

public class ConvertCharToInt {

  public static void main(String[] args) {
    char c = '2';
    char v = '3';
    
    int i = Integer.parseInt(String.valueOf(c));
    int j = Integer.parseInt(String.valueOf(v));
    
    System.out.println("The integer value of '" + c + "' is: " + i);
    System.out.println("The integer value of '" + v + "' is: " + j);

  }

}
The integer value of '2' is: 2
The integer value of '3' is: 3

Subtract with ‘0’

If we subtract the character value from ‘0’, it will convert the value into an integer value. Since subtracting any value from 0 returns the same value.

public class ConvertCharToInt {

  public static void main(String[] args) {
    char c = '2';
    char v = '3';
    
    int i = c - '0';
    int j = v - '0';
    
    System.out.println("The integer value of '" + c + "' is: " + i);
    System.out.println("The integer value of '" + v + "' is: " + j);

  }

}
The integer value of '2' is: 2
The integer value of '3' is: 3

 

Reference

Translate ยป