While doing widening and boxing- (for eg: int - long - Long), we are actually doing casting of one class object to another class. ie, int will box into Integer and it is trying to cast to Long class object. since it is not practical, it will give a compilation error.
But you can use this - int to Number- This is because the conversion is int-Integer - Number and Number is the Super class for Integer. So the program below will work fine:
Thanks<br /> <br />Anoobkumar<br />SCJP 1.5
Joined: Sep 20, 2008
Yes Ankit Garg you are correct...
Here The variable "b" First Gets Widening and then Boxing... so it shows error...this i know very well... But Why this is (First Gets Widening and then Boxing)not allowed.. to do so in Java ... [ October 23, 2008: Message edited by: Thangaraj Raj ]
Well Thangaraj we are just kids in front of developers of Java. If java doesn't support anything, then there must be a reason for it. My first post at javaranch was about a problem which I thought was a bug but at last I was proved wrong. When autoboxing was added to java, the developers of java must have thought about thousands of exceptional cases. There must be a specific case which would create problems if Wedening and then boxing was allowed. Let's see if anybody knows such a case. Otherwise it's running as a background thread in my mind. If I will find any such case, I will definitely tell you.