X - AS Nida
X - AS Nida
X - AS Nida
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
Comparing Strings<br />
Comparing Strings<br />
Comparing Characters<br />
Comparing Characters<br />
Remember that a character string in Java is an object<br />
We can use the relational operators on character data<br />
We cannot use the relational operators to compare<br />
strings<br />
The results are based on the Unicode character set<br />
The equals method can be called with strings to<br />
determine if two strings contain exactly the same<br />
characters in the same order<br />
The following condition is true because the character<br />
+ (Unicode 43) comes before the character J<br />
(Unicode 74) in the Unicode character set:<br />
if ('+' < 'J')<br />
System.out.println ("+ is less than J");<br />
The String class also contains a method called<br />
compareTo to determine if one string comes before<br />
another (based on the Unicode character set)<br />
The uppercase alphabet (A-Z) followed by the<br />
lowercase alphabet (a-z) appear in alphabetical order<br />
in the Unicode character set<br />
26<br />
25<br />
More Operators<br />
More Operators<br />
Lexicographic Ordering<br />
Lexicographic Ordering<br />
In particular, we will examine<br />
• the increment and decrement operators<br />
Because comparing characters and strings is based<br />
on a character set, it is called a lexicographic<br />
ordering<br />
• the assignment operators<br />
This is not strictly alphabetical when uppercase and<br />
lowercase characters are mixed<br />
For example, the string “Box" comes before the<br />
string “art" because all of the uppercase letters<br />
come before all of the lowercase letters in Unicode<br />
Also, short strings come before longer strings with<br />
the same prefix (lexicographically)<br />
Therefore "book" comes before "bookcase"<br />
28<br />
27