19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

2.17 Character Data Type and Operations 63<br />

8 JOptionPane.INFORMATION_MESSAGE);<br />

9 }<br />

10 }<br />

If no Chinese font is installed on your system, you will not be able <strong>to</strong> see the Chinese characters.<br />

The Unicodes for the Greek letters abgare \u03b1 \u03b2 \u03b3.<br />

Most <strong>com</strong>puters use ASCII (American Standard Code for Information Interchange), a 7-bit<br />

encoding scheme for representing all uppercase and lowercase letters, digits, punctuation marks,<br />

and control characters. Unicode includes ASCII code, with \u0000 <strong>to</strong> \u007F corresponding<br />

<strong>to</strong> the 128 ASCII characters. (See Appendix B for a list of ASCII characters and their decimal<br />

and hexadecimal codes.) You can use ASCII characters such as 'X', '1', and '$' in a <strong>Java</strong><br />

program as well as Unicodes. Thus, for example, the following statements are equivalent:<br />

ASCII<br />

char letter = 'A';<br />

char letter = '\u0041'; // Character A's Unicode is 0041<br />

Both statements assign character A <strong>to</strong> the char variable letter.<br />

Note<br />

The increment and decrement opera<strong>to</strong>rs can also be used on char variables <strong>to</strong> get the<br />

next or preceding Unicode character. For example, the following statements display<br />

character b.<br />

char ch = 'a';<br />

System.out.println(++ch);<br />

char increment and<br />

decrement<br />

2.17.2 Escape Characters<br />

Suppose you want <strong>to</strong> print a message with quotation marks in the output. Can you write a<br />

statement like this?<br />

System.out.println("He said "<strong>Java</strong> is fun" ");<br />

No, this statement has a <strong>com</strong>pile error. The <strong>com</strong>piler thinks the second quotation character<br />

is the end of the string and does not know what <strong>to</strong> do with the rest of the characters.<br />

To over<strong>com</strong>e this problem, <strong>Java</strong> uses a special notation <strong>to</strong> represent special characters, as<br />

shown in Table 2.6. This special notation, called an escape character, consists of a backslash<br />

(\) followed by a character or a character sequence. For example, \t is an escape character<br />

for the Tab character and an escape character such as \u03b1 is used <strong>to</strong> represent a Unicode.<br />

The symbols in an escape character are interpreted as a whole rather than individually.<br />

So, now you can print the quoted message using the following statement:<br />

escape character<br />

System.out.println("He said \"<strong>Java</strong> is fun\"");<br />

The output is<br />

He said "<strong>Java</strong> is fun"<br />

Note that the symbols \ and " <strong>to</strong>gether represent one character.<br />

2.17.3 Casting between char and Numeric Types<br />

A char can be cast in<strong>to</strong> any numeric type, and vice versa. When an integer is cast in<strong>to</strong> a<br />

char, only its lower 16 bits of data are used; the other part is ignored. For example:<br />

char ch = (char)0XAB0041; // The lower 16 bits hex code 0041 is<br />

// assigned <strong>to</strong> ch<br />

System.out.println(ch); // ch is character A

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!