This week's giveaway is in the Spring forum.We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!See this thread for details.
Big Moose Saloon
 Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies Register / Login Win a copy of REST with Spring (video course) this week in the Spring forum!

# Short cast to char - bungle

Simon Cockayne
Ranch Hand

Joined: Dec 09, 2000
Posts: 214
Hi,

Why does my_byte (value 56) when cast to a char, givea value of 8?

Cheers,

Si.

SCJP 1.4 (93%)<br />SCJD (In progress. It can run, but it can't hide...)
levani dvalishvili
Ranch Hand

Joined: Mar 01, 2005
Posts: 99
decimal equvalent of char 8 is 56
since you are asigning a byte to a char it assumes a decimal char value and gives you number 8 .
[ April 10, 2005: Message edited by: levani dvalishvili ]

SCJP 1.5(Done) SCJA 1.0(Done)<br />SCWCD(in Progress...)
Simon Cockayne
Ranch Hand

Joined: Dec 09, 2000
Posts: 214
Hi,

Thanks for the response.

Why is "decimal equvalent of char 8 is 56"?

Cheers,

Si.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

The char '8' is represented by the decimal value 56.

See the following ASCII table...
http://www.lookuptables.com/

"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
Saurabh Khanna
Ranch Hand

Joined: Apr 03, 2005
Posts: 30
char is UNSIGNED 16 bit integral type where as byte is signed. The main point is that the 16 bits in char are used to represent characters according to UNICODE character set.

Saurabh
Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
hi
can anyone explain the subtle diff between the char byte and short
Joe Sondow
Ranch Hand

Joined: Apr 10, 2005
Posts: 195
byte: 8-bit signed integer. Range -128 to 127.
short: 16-bit signed integer. Range -32768 to 32767.
char: 16-bit unsigned integer. Range 0 to 65535.

SCJA 1.0 (98%), SCJP 1.4 (98%)
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
Another way of answering is that a char is a 16 bit unsigned number, while a short is a 15 bit number plus a one bit sign on the left. You may want to read up on two's complement binary notation, it is part of the SCJP exam syllabus.

Mike Gershman
SCJP 1.4, SCWCD in process

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

subject: Short cast to char - bungle