Exercise:
Write a Java Program to convert char type variables to int.
1.Convert using ASCII
Click Here to View the Solution!
public class CharToInt {
public static void main(String[] args) {
// create char variables
char c1 = '9';
char c2 = 'e';
// convert char variables to int
// ASCII value of characters is assigned
int num1 = c1;
int num2 = c2;
// print the values
System.out.println(num1);
System.out.println(num2);
}
}
Click Here to View the Output!
57 101
Click Here to View the Explanation!
- In this program, there are two variables
c1 ,c2
of the typechar
. These variables are passed as values for two other integer type variables. - The values assigned to the integer variables here won’t be the characters themselves but their
ASCII
representation. - Thus, the integer variables would contain ‘57’ and ‘101’ which are equivalent to the ASCII of 9 and e respectively.
2.Convert using getNumericValue() method
Click Here to View the Solution!
public class CharToInt {
public static void main(String[] args) {
// create char variables
char c1 = '9';
char c2 = 'j';
// convert char variable to int
// Use getNumericValue()
int num1 = Character.getNumericValue(c1);
int num2 = Character.getNumericValue(c2);
// print the numeric value of characters
System.out.println(num1);
System.out.println(num2);
}
}
Click Here to View the Output!
9 19
Click Here to View the Explanation!
- In this program, a Character class method called
getNumericValue()
is used for the conversion of data types. This method takes up variables in char and returns them in int. - It can be seen that the numeric representation of char type variables is retrieved by using the
getNumericValue()
method. Both the characters 9 and j are converted into their numerical forms with the new type as integer.
3.Convert using parseInt() method
Click Here to View the Solution!
public class CharToInt {
public static void main(String[] args) {
// create char variables
char c1 = '2';
char c2 = '9';
int num1 = Integer.parseInt(String.valueOf(c1));
int num2 = Integer.parseInt(String.valueOf(c2));
// print numeric value
System.out.println(num1);
System.out.println(num2);
}
}
Click Here to View the Output!
2 9
Click Here to View the Explanation!
- In this program, an Integer class method called
parseInt()
is used for the conversion of data types. This method takes up variables in int and returns them in char form. - In the following expression:
Integer.parseInt(String.valueOf(c1))
- The char type variable ‘a’ is first converted to a String type variable using
String.valueOf(c1)
. - Then the converted variable of String type is passed as a parameter in
Integer.parseInt()
method for further conversion into integer type.
- The variable ‘c1’ is first converted to String and then to int since the method
Integer.parseInt()
only takes up String variables as parameters.
4.Convert by subtracting with ‘0’
Click Here to View the Solution!
public class CharToInt {
public static void main(String[] args) {
char c1 = '9';
char c2 = '3';
// convert char variables to int by subtracting with char
int num1 = c1 - '0';
int num2 = c2 - '0';
// print numeric value
System.out.println(num1);
System.out.println(num2);
}
}
Click Here to View the Output!
9 3
Click Here to View the Explanation!
- In this program, conversion from char type to int type takes place by subtracting the character ‘0’ from the given char variable.
- In the line:
int num1 = c1 - '0';
- 0 is being subtracted from the char variable
c1
. Here, both the charactersc1
and ‘0’ are implicitly converted into integer type variables and thus the value stored innum1
will consist of an int value. Since subtracting 0 from any number will return the number itself, thusnum1 and num2
contain the integer values 9 and 3 from their corresponding char values ‘9’ and ‘3’.