To convert a char
to an int
in Java, you can use the Character.getNumericValue(char ch)
method or cast the char
to an int
.
Here is an example using the Character.getNumericValue()
method:
char ch = '5'; int num = Character.getNumericValue(ch); System.out.println(num); // Output: 5
And here is an example using casting:
char ch = '5'; int num = (int) ch - '0'; System.out.println(num); // Output: 5
In the second example, we subtract the character code for ‘0’ from the character code of the char
to get the integer value. This works because the character codes for the digits 0 through 9 are contiguous in the ASCII table. So, for example, the character code for ‘5’ is 53, and the character code for ‘0’ is 48, so 53 - 48
equals 5.
1) Java char to int Example: Get ASCII value
To get the ASCII value of a char
in Java, you can simply cast the char
to an int
type. The resulting int
value will be the ASCII code for the corresponding character.
Here is an example:
char ch = 'A'; int ascii = (int) ch; System.out.println(ascii); // Output: 65
In this example, we cast the char
‘A’ to an int
, resulting in the ASCII code for ‘A’, which is 65.
Note that Java uses Unicode character encoding, which is an extension of ASCII. As a result, the ASCII values for the first 128 characters are the same in Unicode. Therefore, the above method works for getting the Unicode code point for any character in the ASCII range. However, for characters outside the ASCII range, the resulting int
value will be the Unicode code point for the character.
2) Java char to int Example: Character.getNumericValue()
In addition to getting the ASCII value of a char
in Java, you can also use the Character.getNumericValue(char ch)
method to get the numeric value of a char
. This method returns the numeric value of the character as an int
.
Here is an example:
char ch = '5'; int num = Character.getNumericValue(ch); System.out.println(num); // Output: 5
In this example, we use the Character.getNumericValue()
method to get the numeric value of the character ‘5’, which is 5.
It is important to note that this method only returns the numeric value for characters that are digits, such as ‘0’ through ‘9’. For other characters, it returns a value of -1. Therefore, this method is useful when you want to convert a digit character to its corresponding numeric value, but it is not appropriate for converting other types of characters to int
.
3) Java char to int Example: String.valueOf()
Another way to convert a char
to an int
in Java is by using the String.valueOf(char ch)
method. This method returns a String
representation of the char
argument, which can then be parsed as an int
.
Here is an example:
char ch = '7'; int num = Integer.parseInt(String.valueOf(ch)); System.out.println(num); // Output: 7
In this example, we first convert the char
to a String
using the String.valueOf()
method. Then, we use the Integer.parseInt()
method to parse the String
as an int
. The resulting value is the integer value of the original char
.
This method is useful when you need to convert a char
to an int
in a context where a String
value is expected. However, for simple conversions where you just need the integer value of the char
, it is simpler to use one of the previous methods shown.