File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes chars Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "chars" Watch "chars" New topic
Author

chars

Dan Silva
Ranch Hand

Joined: Sep 05, 2007
Posts: 86
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.
Piotr Dr
Greenhorn

Joined: Jan 02, 2008
Posts: 3
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).
Piotr Dr
Greenhorn

Joined: Jan 02, 2008
Posts: 3
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';
Dean Jones
Ranch Hand

Joined: Dec 29, 2007
Posts: 129
Dear Piotr,

Can you please throw some light on how to represent the following symbols using character form:

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';
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Howdy "Piotr Dr" !

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.


Yours,
Bu.


all events occur in real time
Satya Maheshwari
Ranch Hand

Joined: Jan 01, 2007
Posts: 368
Different ways to define char literals:



Thanks and Regards
Sandeep Bhandari
Ranch Hand

Joined: Apr 16, 2004
Posts: 201
Also look at this:

long \u0062 = 10;
is valid

long \u0090 = 10;

is invalid


SCJP 96% | SCWCD 90%| SCJP mock exams | My SCJP blog
O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

Why does this not need a cast

char d = (char) -98

but this does need a cast

char e = -29 ?

Scotty Mitchell
Ranch Hand

Joined: Aug 09, 2011
Posts: 46
O. Ziggy wrote:Why does this not need a cast

char d = (char) -98

but this does need a cast

char e = -29 ?



That is casted theres a (char) cast right in front of it.
O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

Scotty Mitchell wrote:
O. Ziggy wrote:Why does this not need a cast

char d = (char) -98

but this does need a cast

char e = -29 ?



That is casted theres a (char) cast right in front of it.


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: chars