Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

chars

 
Dan Silva
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Satya Maheshwari
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Different ways to define char literals:

 
Sandeep Bhandari
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also look at this:

long \u0062 = 10;
is valid

long \u0090 = 10;

is invalid
 
O. Ziggy
Ranch Hand
Posts: 430
Android Debian VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why does this not need a cast

char d = (char) -98

but this does need a cast

char e = -29 ?

 
Scotty Mitchell
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 430
Android Debian VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic