File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
A friendly place for programming greenhorns!
Big Moose Saloon
Register / Login
Programmer Certification (SCJP/OCPJP)
Casting int to byte (was: pls explain)
vikram choudhary .
Joined: Apr 25, 2007
Apr 25, 2007 22:14:00
byte i= (byte)129;
output: -127 why?
can anyone explain?
[ April 25, 2007: Message edited by: Barry Gaunt ]
Joined: Feb 23, 2007
Apr 25, 2007 22:21:00
byte range is -128 to 127
if you assign any value out of this range it will give compile time error
if you type cast to byte like this
it will print -126
when it is out of range it again start from -128
so her the range exceeds by 3
Joined: Feb 28, 2007
Apr 25, 2007 22:31:00
so her the range exceeds by
3 or 2?
A byte has 8 bit capacity, the most significant bit is for sign and all other
seven LSB (least significant bits) are for value.
If the limit exceeds, all the top bits of the the right hand side(int literal)
value is discarded.
Joined: Aug 03, 2002
Apr 25, 2007 22:38:00
To avoid confusion please continue in
Ask a Meaningful Question
Getting someone to think and try something out is much more useful than just telling them the answer.
I agree. Here's the link:
subject: Casting int to byte (was: pls explain)
isn't this strange?
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter
| Powered by
Copyright © 1998-2014