love demgracy, knowledge demogracy, open source and Java - OCPJP 76%
Stephan van Hulst wrote:Nope. Autoboxing only works between a wrapper class and its respective primitive. If you want to box an int to a Byte or a Character, either cast it to byte or char first, or even better, use the wrapper class' valueOf method.
[edit]
Apparently not. Care to shed some light on this, Henry? I get the same error as the original poster, and it won't even compile if I try to store i into a Long.
love demgracy, knowledge demogracy, open source and Java - OCPJP 76%
Imad Aydarooos wrote:Dear Henry Wong , my practice shows it will cause error at runtime.
Stephan van Hulst wrote:Apparently not. Care to shed some light on this, Henry? I get the same error as the original poster, and it won't even compile if I try to store i into a Long.
OCPJP 95%
Henry Wong wrote:
Imad Aydarooos wrote:Dear Henry Wong , my practice shows it will cause error at runtime.
Don't know what to tell you... I cut and pasted your code. Compiled it. Ran it. No errors.... BTW, I am using Java 6, in case you are interested.
Henry
love demgracy, knowledge demogracy, open source and Java - OCPJP 76%
Life is either daring something or nothing - Helen Keller
Bharath Raja wrote:compiles, and run fine for me.
seems very close to previous thread
Adam Michalik wrote:I have posted a bug to the Sun Java Bug Database: Bug ID = 6816548.
love demgracy, knowledge demogracy, open source and Java - OCPJP 76%
Pradeep Chandra wrote:compiles and run fine for me too.
BTW if you remove final modifier from int i then it will generate error.
why?
I am not sure but i think that making final to a code will fix its value which in future cann't be changed.
If not changed and the value is in range of byte then it compiles successfully.
also try with
final int i = -30;
Byte b = i; // will compile fine
Character c = i;//will generate error as i can't be changed and it is negative. so, it is not under the range of character.
Please provide view on final and its casting
Thanks
love demgracy, knowledge demogracy, open source and Java - OCPJP 76%
Henry Wong wrote:
Imad Aydarooos wrote:Dear Henry Wong , my practice shows it will cause error at runtime.
Don't know what to tell you... I cut and pasted your code. Compiled it. Ran it. No errors.... BTW, I am using Java 6, in case you are interested.
Henry
love demgracy, knowledge demogracy, open source and Java - OCPJP 76%
What are you doing in my house? Get 'em tiny ad!
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|