Hi, I'm about to take the SCJP in the next few months and is reading a chapter in the book about autoboxing/unboxing. Having read the chapter, I decided to try it out in code and found something that I do not quite understand...
These lines apparently fails to compile: short s = 2; Integer x = (short)s; //cannot convert from short to Integer
byte b = 2; Integer y = (byte)b; //cannot convert from byte to Integer
Long lx = (int)32; //cannot int to Long. Long lx = 32 doesn't work either.
However, these actually worked:
Integer x = (short)2; Integer y = (byte)2;
Why is this? Why does "casting" an int with short or byte allowed it to be converted? Then why doesn't Long lx = (int)32; work?
[ June 30, 2007: Message edited by: Hugh Kwong ] [ June 30, 2007: Message edited by: Hugh K ]
The book wasn't so clear about the behavior of boxing/unboxing at assignments (it only covered the box-->widening on overloading..), so I thought it might have worked. And to add to the confusion, I was using Eclipse 3.2.x to compile... [ July 01, 2007: Message edited by: Hugh K ]