This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
What do I need to know about chars for the SCJP exam? If someone could help me know what kind of problems I'm supposed to be able to do with regards to chars, it would be much appreciated.
SCJP Sun Certified Programmer for Java 5 Study Guide:
Character Literals A char literal is represented by a single character in single quotes.
char a = 'a'; char b = '@';
You can also type in the Unicode value of the character, using the Unicode notation of prefixing the value with \u as follows:
char letterN = '\u004E'; // The letter 'N'
Remember, characters are just 16-bit unsigned integers under the hood. That means you can assign a number literal, assuming it will fit into the unsigned 16-bit range (65535 or less). For example, the following are all legal:
char a = 0x892; // hexadecimal literal char b = 982; // int literal char c = (char)70000; // The cast is required; 70000 is // out of char range
char d = (char) -98; // Ridiculous, but legal
And the following are not legal and produce compiler errors:
char e = -29; // Possible loss of precision; needs a cast char f = 70000 // Possible loss of precision; needs a cast
You can also use an escape code if you want to represent a character that can't be typed in as a literal, including the characters for linefeed, newline, horizontal tab, backspace, and single quotes.
char c = '\"'; //A double quote char d = '\n'; //A newline
The char type (a character) contains a single, 16-bit Unicode character. Although the extended ASCII set known as ISO Latin-1 needs only 8 bits (256 different characters), a larger range is needed to represent characters found in languages other than English. Unicode characters are actually represented by unsigned 16-bit integers, which means 216 possible values, ranging from 0 to 65535 (216)-1, You'll learn in Chapter 3 that because a char is really an integer type, it can be assigned to any number type large enough to hold 65535 (which means anything larger than a short. Although both chars and shorts are 16-bit types, remember that a short uses 1 bit to represent the sign, so fewer positive numbers are acceptable in a short).
Piotr Dr
Greenhorn
Joined: Jan 02, 2008
Posts: 3
posted
0
It is important to remember how char is related with integer (casting). For instance:
char a = 65;//implict cast by compilator: char a = (int) 65; char a = 65536;//compiler time error: to much for char
The escape characters are quite tricky:
-escape character in Java: \t The tab character ('\u0009') \n The newline (line feed) character ('\u000A') \r The carriage-return character ('\u000D') \f The form-feed character ('\u000C') \a The alert (bell) character ('\u0007') \e The escape character ('\u001B')
and literal ( almost all ):
\" \' \\
Also tricky is initialisation char. You can use:
0n The character with octal value 0n (0 <= n <= 7)
0nn The character with octal value 0nn (0 <= n <= 7)
0mnn The character with octal value 0mnn (0 <= m <= 3, 0 <= n <= 7) // char A = 0101
xhh The character with hexadecimal value 0xhh // char A = 0x41;
\uhhhh The character with hexadecimal value 0xhhhh // char A = '\u0041';
There aren't many rules to follow on the ranch, but one is about the use of propper display names.
Your name does not follow this rule. The main reasons why and a link how to change yours you'll find here: http://www.javaranch.com/name.jsp
So, could you please change your user name before your next posting? It will not affect anything you've already posted here. Just your user name will update.
I'm writing this because I am one of the moderators of this forum.