Page 472 - Introduction to Programming with Java: A Problem Solving Approach
P. 472

                438 Chapter 11 Type Details and Alternate Coding Mechanisms 11.3 char Type and the ASCII Character Set
This section supplements the char type material you studied in Chapter 3, Section 3.20. Underlying Numeric Values
For most programming languages, including Java, each character has an underlying numeric value. For ex- ample, the character ‘A’ has the underlying value of 65 and the character ‘B’ has the underlying value of 66. Most programming languages, including Java, get character numeric values from the American Standard Code for Information Interchange (ASCII, pronounced “askee”) character set. See the ASCII character set in Figure 11.4’s ASCII table and confirm that the character ‘A’ has an underlying value of 65.
So what’s the point of having underlying numeric values for characters? With underlying numeric val- ues, it makes it easier for the JVM to determine the ordering of characters. For example, since ‘A’ has the value 65 and ‘B’ has the value 66, the JVM can easily determine that ‘A’ comes before ‘B’. And knowing the order of characters is necessary for string sort operations. For example, suppose a sort method is given the strings “peach”, “pineapple”, and “apple.” The sort method compares the words’ first characters ‘p’, ‘p’, and ‘a’, and in doing so, the JVM looks up the characters in the ASCII table. Since ‘p’ has the value 112 and ‘a’ has the value 97, “apple” goes first. Then the sort method compares the second characters in “peach” and “pineapple.” Since e has the value 101 and i has the value 105, “peach” goes before “pineapple.”
Most characters in the ASCII character set represent printable symbols. For example, the ‘f’ character represents the printable letter f. But the first 32 characters and the last character in the ASCII character set are different—they are control characters. Control characters perform non-printing operations. For ex-
 {
}
ch = (char) code;
System.out.print(ch + " ");
Apago PDF Enhancer
ample, the start-of-heading character (ASCII numeric value 1) helps with data being sent from one computer device to another. More specifically, it signals the beginning of transmitted data. When you print a control character, you might be surprised by what appears on the screen. The bell character (ASCII numeric value 7) normally generates a sound and displays nothing, which makes sense, but the start-of-heading character displays something less intuitive. When you print the start-of-heading character, you’ll get different results in different environments. For example, in a console window1 in a Windows environment, a smiley face is displayed. In other environments, a blank square is displayed. Note the following code fragment, with as- sociated output from a console window in a Windows environment:
char ch;
for (int code=1; code<=6; code++)
Output:
☺☻♥♦♣ ♠
In the above code fragment, the (char) cast operator uses the ASCII table to return the character associated with code’s numeric value. So if code has the value 1, then (char) code returns the start-of- heading character.
  1 See Chapter 1’s “First Program—Hello World” section for a description of how to run a program in a console window.


















































































   470   471   472   473   474