When you attempt to compile the above code you'll get one error:
ByteExample.java:10: Incompatible type for declaration. Explicit cast needed to convert int to byte. byte abb=2000;
from A Programmer's Guide to Java Certification by Mughal and Rasmussen:
...implicit narrowing primitive conversions on assignment can occur in cases where the souce is an int constant expression whose value can be determined to be in the range of the destination type at compile time; the destination type is either byte, short, or char type.
...all other narrowing primitive conversions will produce a compile error on assignment and will explicity require a cast.
With those rules in mind -- lets look at your statement again:
byte abb = 2000;
* The RHS (right hand side) is a constant int expression
* The LHS (left hand side) is a byte
* The RHS is
not within range of the LHS.
(a byte's range is -128 to 127)] So.... that means you need to do an explicit cast:
byte abb = (byte)2000; ... of course, when you do that because 2000 is out of the range of a byte the number will spill over to the negative side and you end up with
-48!! [ November 22, 2002: Message edited by: Jessica Sant ]