wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Literals 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 "Literals" Watch "Literals" New topic
Author

Literals

Sarath Koiloth Ramath
Ranch Hand

Joined: May 07, 2008
Posts: 52
Hi Friends,

I have a doubt basic regarding the literals,As char is 16 bit.I found in K&B book that char c=(int)700000 is a valid statement.How it is working,and is it storing as a character or as an interger.

I know its a very basic question.


Thank You


J 4 Java
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

As per i know, char are stored as 16 bit unsigned integer. So int from 0 to 2(raise to power 16) -1, can be assigned to a char as it is. Apart from that you need to cast explicitly.


Thanks and Regards,
SCJP 1.5 (90%), SCWCD 1.5 (85%), The Jovial Java, java.util.concurrent tutorial
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

As a 32-bit int, 70000 is...

0000 0000 0000 1010 1010 1110 0110 0000

When this value is cast to type char, only the rightmost 16 bits are preserved...

1010 1110 0110 0000

In hex, this is AE60. Consulting with Unicode.org, you will find that this value represents a Hangul Syllable.

More than likely, the environment in which you're running Java (for example, a command prompt application) will not know how to display this value as a Unicode character, so you might see something like a question mark displayed instead.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Sarath Koiloth Ramath
Ranch Hand

Joined: May 07, 2008
Posts: 52
Thank you for your valuable replies.
venu surampudi
Greenhorn

Joined: Jan 03, 2008
Posts: 16
Interesting but i could not get it to run. I get the following exception

Main.java:102: possible loss of precision

and i also think this is kind of understantable as we are trying to stuff a 32 bit thing ( for lack of vocabulary ) into a 16 bit thing

am is missing something? clearly i am


If you wanna be cocky you better be kobe !
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by venu surampudi:
...am is missing something? clearly i am

I think there was a typo in the original post. The literal 700000 is already an int, so the cast should be to type char instead of int...

char c = (char)700000;
Dean Jones
Ranch Hand

Joined: Dec 29, 2007
Posts: 129
It prints a question mark since it is outside the range of char.(0-255)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Literals
 
Similar Threads
Char esc
char literals
SIGNED AND UNSIGNED
narrowing conversion
question on conversion