Originally posted by Roy Ben Ami:
do u agree that this is legal?
byte b=5;
this works right? because although 5 is an int it gets casted automatically when u use = to a byte.
(the only case it does).
when u declare an int final it is the same for the comoiler as writing a constant like 5 (cause final makes it a constant too)!
so
final int x=5;
byte b=x;
works the same as the example before (casting happens automatically).
u always need to cast if it a narrowing cast (ie from short to byte, int to byte, short to byte, long to int etc...) BUT in the case u need to put a constant number in a byte a short or a char etc u dont need to cast , the compiler does that for u.
Originally posted by chung lee:
OK, I'm now going home before I start to lose my sanity.
If anyone would like to have a stab at this then please go ahead. There's no shame in getting things wrong (honest!).
I just hope that this type of question doesn't appear in my exam 59 times.
chung
Originally posted by Roy Ben Ami:
and according to the jls this supposed to work for int, short char and byte!!! not just short and int like corey example shows!
this is truly weird.......
im stumped as well guys.
TADA! riddle solved!
Rob
SCJP 1.4
Originally posted by Paul Salerno:
Im a little confused about these posts:
Regarding casting not required for ints only Rob stated first this wont compile
Then in the last post, all of the sudden this is legal. Whats up with that. What is the actual rule regarding ALL of the primitive types, excluding boolean. I'd appreciate it, cause I know a question like this is going to be on the test.
Rob
SCJP 1.4
Yet Another SCJP2