Different method to convert "Char" to "int" in Java
To convert a char to int java uses multiple ways, there is no one specific way, it can be done by using many methods java provides.
There are 3 methods explained in this tutorial:
- ASCII values.
- getNumericalValue.
- ParseInt using ValueOf method.
These methods are explained in the article
Method-1: Use ASCII values
The first way of conversion of char to int java the char variable is assigned to an integer variable, this method returns the ASCII value of the character variable. A demonstration is shown in the code below:
import java.util.*;
public class main {
// main function
public static void main(String[] args) {
char character_variable1 = 'A';
char character_variable2 = 'B';
char character_variable3 = 'C';
char character_variable4 = 'D';
// char to int java conversion
int integer_variable1 = character_variable1;
// char to int java conversion
int integer_variable2 = character_variable2;
// char to int java conversion
int integer_variable3 = character_variable3;
// char to int java conversion
int integer_variable4 = character_variable4;
System.out.println("the value of integer variable 1 would be the ASCII of " + character_variable1 + " i.e : " + integer_variable1);
System.out.println("the value of integer variable 2 would be the ASCII of " + character_variable2 + " i.e : " + integer_variable2);
System.out.println("the value of integer variable 3 would be the ASCII of " + character_variable3 + " i.e : " + integer_variable3);
System.out.println("the value of integer variable 4 would be the ASCII of " + character_variable4 + " i.e : " + integer_variable4);
}
}
The output of this code is
the value of integer variable 1 would be the ASCII of A i.e : 65
the value of integer variable 2 would be the ASCII of B i.e : 66
the value of integer variable 3 would be the ASCII of C i.e : 67
the value of integer variable 4 would be the ASCII of D i.e : 68
Even if the character variable has a numerical value, its ASCII will be displayed in this method, one way to convert char into int java, when char value is numerical is to subtract 48 from them. Like in the code below:
import java.util.*;
public class main {
//main function
public static void main(String[] args) {
char character_variable1 = '1';
char character_variable2 = '2';
char character_variable3 = '3';
char character_variable4 = '4';
//char to int java conversion
int integer_variable1 = character_variable1 - 48;
//char to int java conversion
int integer_variable2 = character_variable2 - 48;
//char to int java conversion
int integer_variable3 = character_variable3 - 48;
//char to int java conversion
int integer_variable4 = character_variable4 - 48;
System.out.println("the value of integer variable 1 would be: " + integer_variable1);
System.out.println("the value of integer variable 2 would be : " + integer_variable2);
System.out.println("the value of integer variable 3 would be : " + integer_variable3);
System.out.println("the value of integer variable 4 would be : " + integer_variable4);
}
}
The output of the following code will be
the value of integer variable 1 would be : 1
the value of integer variable 2 would be : 2
the value of integer variable 3 would be : 3
the value of integer variable 4 would be : 4
The same can be done by subtracting char ‘0’ from the character asci value, the code below demonstrates how:
import java.util.*;
public class main {
// main function
public static void main(String[] args) {
char character_variable1 = '1';
char character_variable2 = '2';
char character_variable3 = '3';
char character_variable4 = '4';
// char to int java conversion
int integer_variable1 = character_variable1 - '0';
// char to int java conversion
int integer_variable2 = character_variable2 - '0';
// char to int java conversion
int integer_variable3 = character_variable3 - '0';
// char to int java conversion
int integer_variable4 = character_variable4 - '0';
System.out.println("the value of integer variable 1 would be : " + integer_variable1);
System.out.println("the value of integer variable 2 would be : " + integer_variable2);
System.out.println("the value of integer variable 3 would be : " + integer_variable3);
System.out.println("the value of integer variable 4 would be : " + integer_variable4);
}
}
The output of this code is
the value of integer variable 1 would be : 1
the value of integer variable 2 would be : 2
the value of integer variable 3 would be : 3
the value of integer variable 4 would be : 4
But since this method is ineffective we use character.getNumericalValue()
method.
Method-2 : Character.getNumericalValue()
This method is of the character class and it returns an integer value of the number assigned to a character variable. The example is shown in the code below :
import java.util.*;
public class main {
// main function
public static void main(String[] args) {
char character_variable1 = '1';
char character_variable2 = '2';
char character_variable3 = '3';
char character_variable4 = '4';
// use the method Character.getNumericValue() for char to int java conversion
int integer_variable1 = Character.getNumericValue(character_variable1);
// use the method Character.getNumericValue() for char to int java conversion
int integer_variable2 = Character.getNumericValue(character_variable2);
// use the method Character.getNumericValue() for char to int java conversion
int integer_variable3 = Character.getNumericValue(character_variable3);
// use the method Character.getNumericValue() for char to int java conversion
int integer_variable4 = Character.getNumericValue(character_variable4);
System.out.println("the value of integer variable 1 would be : " + integer_variable1);
System.out.println("the value of integer variable 2 would be : " + integer_variable2);
System.out.println("the value of integer variable 3 would be : " + integer_variable3);
System.out.println("the value of integer variable 4 would be : " + integer_variable4);
}
}
The output of the following code will be
the value of integer variable 1 would be : 1
the value of integer variable 2 would be : 2
the value of integer variable 3 would be : 3
the value of integer variable 4 would be : 4
Method-3 : Integer.ParseInt(String) using String.valueOf() method
The Integer.ParseInt()
method takes a string as a parameter and returns an integer value of the parameter provided, while the String.valueOf
method is used to convert any datatype i.e. int, char, double, float into a string. So to use this method for the char to int java conversion, we first convert the character variable into a string, using the String.valueOf()
method, then we convert it into an integer using Integer.ParseInt(String)
method. The code below demonstrates how.
import java.util.*;
public class main {
// main function
public static void main(String[] args) {
char character_variable1 = '1';
char character_variable2 = '2';
char character_variable3 = '3';
char character_variable4 = '4';
// use the parseInt and valueOf method for char to int java conversion
System.out.println("the value of integer variable 1 would be : " + Integer.parseInt(String.valueOf(character_variable1)));
// use the parseInt and valueOf method for char to int java conversion
System.out.println("the value of integer variable 2 would be : " + Integer.parseInt(String.valueOf(character_variable2)));
// use the parseInt and valueOf method for char to int java conversion
System.out.println("the value of integer variable 3 would be : " + Integer.parseInt(String.valueOf(character_variable3)));
// use the parseInt and valueOf method for char to int java conversion
System.out.println("the value of integer variable 4 would be : " + Integer.parseInt(String.valueOf(character_variable4)));
}
}
The output of the following code is
the value of integer variable 1 would be : 1
the value of integer variable 2 would be : 2
the value of integer variable 3 would be : 3
the value of integer variable 4 would be : 4
Practice Code
This code is to practice all the methods discussed above, dry run the code, and run it afterward
import java.util.*;
public class main {
// main function
public static void main(String[] args) {
char character_variable1 = '1';
char character_variable2 = '2';
char character_variable3 = '3';
char character_variable4 = '4';
int integer_variable1 = Character.getNumericValue(character_variable1);
int integer_variable2 = 0;
int integer_variable3 = character_variable3 - 48;
int integer_variable4 = character_variable4 - '0';
System.out.println("the value of integer variable 1 would be : " + integer_variable1);
System.out.println("the value of integer variable 2 would be : " + Integer.parseInt(String.valueOf(character_variable2)));
System.out.println("the value of integer variable 3 would be : " + integer_variable3);
System.out.println("the value of integer variable 4 would be : " + integer_variable4);
}
}
Summary
The first way of conversion of char to int java the char variable is assigned to an integer variable, this method returns the ASCII value of the character variable. A demonstration is shown in the code. Even if the character variable has a numerical value, its ASCII will be displayed in this method, one way to convert char into int java, when char value is numerical is to subtract 48 from them.
Another way is the Character.getValue()
.This method is of the character class and it returns an integer value of the number assigned to a character variable. The Integer.ParseInt()
method takes a string as a parameter and returns an integer value of the parameter provided, while the String.valueOf method is used to convert any datatype into a string.
Further Reading
To further read about the char to int java conversion click on the links provided below:
Java Character
Java datatype conversion