Piotr Dr

Greenhorn
+ Follow
since Jan 02, 2008
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Piotr Dr

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';
It's good to know:

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).