Say that state
is 1 and current
is 'm'.
Does the following evaluate to true
or false?
( state==1 && current >= 'a' && current <= 'y')
true
The set of characters that Java uses are arranged
in a sequence, called the collating sequence
.
Every character can be compared to any character.
Character 'a' comes earlier in the sequence than 'z'.
'a' < 'z'
returns true'a' > 'z'
returns false'a' <= 'z'
returns true'a' >= 'z'
returns false'a' == 'z'
returns falseThis is useful if you wish to test for a range of characters. Here is part of the collating sequence for ASCII:
. . . 0123456789 . . . ABCDEFGHIJKLMNOPQRSTUVWXYZ . . . abcdefghijklmnopqrstuvwxyz . . .
There are non-alphabetic characters between the sequences '0' to '9', 'A' to 'Z', and 'a' to 'z'. It is good programming practice to test for ranges of characters only within one of these sequences.
current >= 'a' && current <= 'y'
is goodcurrent >= 'A' && current <= 'y'
is poor practicecurrent >= '0' && current <= '9'
is goodcurrent >= '0' && current <= 'A'
is poor practice
Does '1' < '5'
evaluate to true or false?