char

This type is a 16-bit unsigned quantity that is used to represent text characters. You'd use this to hold a single character. If you want to store several successive characters, such as a name or an address or phone number, you would use the predefined type String, which keeps track of multiple consecutive characters at once. String is a class in the java.lang package, not a primitive type. We'll get to String a little later.

The type char has been made 16 bits long because that's what you need to support character sets from every locale in the world. The USA can get by with just 7-bit ASCII, and Western Europe is fine with 8-bit ISO 8859-1 characters giving a few accented characters. Many other places such as China, Korea, India, and Japan need and use 16-bit character sets. So Java does too. If you are using a computer in the 8-bit western world, characters are automatically converted to/from 16 bits on the way in and out of Java.

Although char holds a text character, inside the JVM char is just a 16-bit binary number. So all the arithmetic operators are available on type char. But unlike all the other arithmetic types, char is unsigned—it never takes a negative value. You should only use char to hold character data or bit values. If you want a 16-bit quantity for calculations, don't use char, use short. If you ignore this advice, a cast (conversion) of a negative value into char will make the value magically become positive without the bits changing. If you cast that char value back to an int, the int will now have a positive value. That's a surefire way to introduce bugs into your code.

example declaration:

char testGrade;
char middleInitial;

range of values: a value in the Unicode code set 0 to 65,535

You have to cast a 32-or 64-bit result if you want to put it into a smaller result. This means that assignments to char must always be cast into the result if they involve any arithmetic. An example would be:

char c = (char) (c + 13); // the cast is required

literals: Char literals are always between single quotes. String literals are always between double quotes, so you can tell apart a one-character String from a char. A char variable can be assigned directly to all of the arithmetic types: byte, short, int, long, float and double. It's not clear why you would want to assign a char literal to a double, but you can. Here are the four ways you can write a char literal:

  • A single character in single quotes, e.g.

    char tShirtSize = 'L';

  • A character escape sequence. The allowable values are:

    ' ' (linefeed)

    ' ' (carriage return)

    'f' formfeed

    '' (backspace)

    ' ' (tab)

    '' (backslash)

    '"' (double quote)

    ''' (single quote)

  • An octal escape sequence. You probably will never use these. See the following box.

  • A Unicode escape sequence. You probably will never use these. See the following box.

Choosing Unicode for the Java char type was a very sensible design choice. Although a few people complained about the cost and waste of using 16 bits per character, these are the same misers who a few years ago didn't want to shift from two-digit years to four-digit years, and who still hold onto their abacuses while they evaluate the “cutting edge” slide rule technology.

Designers of forward-looking systems like Java have a responsibility to include proper support for more than just Western alphabets. Apart from anything else, customers are more likely to buy your software if it supports their native language. Until all systems have adopted Unicode, however, handling it on an ASCII system is clumsy, so avoid sprinkling Unicode literals through your code.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.146.37.250