English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this program, we will learn how to convert a character (char) type variable to an integer (int) in Java.
To understand this example, you should understand the followingJava programmingTopic:
class Main { public static void main(String[] args) { //Create character variables char a = '5; char b = 'c'; //Convert char variable to int //Assign char variable to int type variable int num1 = a; int num2 = b; // print the values System.out.println(num1); // 53 System.out.println(num2); // 99 } }
In the above example, we have char type variables a and b. Note that we have assigned the char variable to an int variable.
In this case, instead of the character, the ASCII value of the character is assigned to an int variable. Therefore, we get the value 53(ASCII value “ 5)And 99(ASCII value “ c”)As output.
We can also use the getNumericValue() method of the Character class to convert char type variables to int type.
class Main { public static void main(String[] args) { //Create character variables char a = '5; char b = '9; //Convert char variable to int // Use the getNumericValue() method int num1 = Character.getNumericValue(a); int num2 = Character.getNumericValue(b); //Print the numeric value of the character System.out.println(num1); // 5 System.out.println(num2); // 9 } }
Here, as we can see, the getNumericValue() method returns the numeric value of the character. The character ' '5' will be converted to an integer 5and the character ' '9' will be converted to an integer 9.
We can also use the parseInt() method of the Integer class to convert char type variables to int type.
class Main { public static void main(String[] args) { // Create character variables char a = '5; char b = '9; // Convert char variable to int // Use the parseInt() method int num1 = Integer.parseInt(String.valueOf(a)); int num2 = Integer.parseInt(String.valueOf(b)); // Print the number System.out.println(num1); // 5 System.out.println(num2); // 9 } }
Note the expression
Integer.parseInt(String.valueOf(a))
Here,
String.valueOf(a) - Convert char type variable a to String type
Integer.parseInt() - Convert string to int
NoteThe Integer.parseInt() method is only applicable to string variables. Therefore, the character 'a' can be converted to a String (string).
In Java, we can also convertSubtract 0 from char characterCharacter to convert it to an integer. For example,
class Main { public static void main(String[] args) { //Create character variables char a = '9; char b = '3; //Convert char variable to int //Subtract character 0 from char character int num1 = a - "0"; int num2 = b - "0"; // print numeric value System.out.println(num1); // 9 System.out.println(num2); // 3 } }
In the above example, please note the following lines:
int num1 = a -"0";
In this case, the character will be converted to an integer. By subtracting zero from the value, the same value will be obtained. That is9 - 0 = 9.
Therefore, we separately obtain the character " 9" and " 3The integer value of "9And3.