Conversion to Integers
All chars have a matching numeric value1. 'a' is 97, 'b' is 98,
'&' is 38, and so on.
Same as assigning an int to a double, you can perform a widening conversion
by attempting to assign a char to an int.
void main() {
int valueOfA = 'a';
IO.println(valueOfA);
}
chars will be automatically converted to ints when used with mathmatical operators like +, -, >, <, etc.
void main() {
char gee = 'g';
// all the letters from a to z have consecutive numeric values.
boolean isLetter = gee >= 'a' && gee <= 'z';
IO.println(isLetter);
}
This can be useful if you are stranded on Mars2 or if you want to see if a character is in some range.
-
You can find some of these values in an "ASCII Table." ↩