This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes char representation question -- was please guide Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "char representation question -- was please guide" Watch "char representation question -- was please guide" New topic
Author

char representation question -- was please guide

ramya ray
Ranch Hand

Joined: Aug 11, 2006
Posts: 101
I was doing the self test from SCJP 1.4 k&b book,i found a question:

Which are valid declaration for char:

A. char c1 = 064770;
B. char c3 = 0xbeef;
and so on...

I am concerned about how to attempt such type of questions.
Do i need to convert first octal represenation to decimal and then choose the option.

is not it difficult and time consuming...
or is there some easy way to do this...
Please Guide


[EDIT: slight change to subject title]
[ September 08, 2006: Message edited by: Henry Wong ]
Tom Adams
Ranch Hand

Joined: Feb 07, 2003
Posts: 56
Char supports a 16-bit unicode character. Under the covers it is really a 16 bit (unsigned) integer and can therefore be assigned like your examples directly to an integer value. The trick is the integer value must be in its range or you would get a compiler error.

The largest number it can hold is 2^16 - 1 (^ as in power of not the bit operator ) which is 65535.

When you see the hex assignment it is simple the largest hex value will be 0xffff - because we only have two bytes. So, 0xbeef is fine.

For the octal number I don't know of a short cut other than converting the number to decimal and compare it to 65535. You could memorize that 65535 in octal is 177777 so any octal number <= is ok.

Any numeric assignment to a char could be cast to a char and get past the compiler in this case the char will only contain the right two bytes.

char c = (char)0xFFFFFFFFFF0041L; // what char does c hold?

HTH
[ September 08, 2006: Message edited by: Tom Adams ]

Tom
ramya ray
Ranch Hand

Joined: Aug 11, 2006
Posts: 101
Thanks a lot Tom,for such a brief description .
I really really appreciate ..Thanks again
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: char representation question -- was please guide
 
Similar Threads
help me
GC question from Khalid
String Replacement problem
ch10 q2
Valid declarations?