| Author |
Widening beats boxing?
|
Duran Harris
Ranch Hand
Joined: Nov 09, 2008
Posts: 571
|
|
|
Isn't it true that widening beats boxing?
|
===>SCJP 1.5(72%)<===
==>SCWCD1.5(76%)<===
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
|
Yes it is true. This is done to maintain the behavior of legacy code when there was no auto boxing...
|
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
|
 |
Himanshu Gupta
Ranch Hand
Joined: Aug 18, 2008
Posts: 598
|
|
|
web page This will help you a lot.
|
My Blog SCJP 5 SCWCD 5
|
 |
Duran Harris
Ranch Hand
Joined: Nov 09, 2008
Posts: 571
|
|
Question 13 from K&B's MasterExam 3 The explanation says that the second invocation of doX , boxes from int to Integer.Shouldn't the 2 ints just be widened to longs(because widening beats boxing)?
|
 |
Duran Harris
Ranch Hand
Joined: Nov 09, 2008
Posts: 571
|
|
Aaah damn...I thought they were longs..not Longs. OK problem solved
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
Originally posted by Duran Harris: Shouldn't the 2 ints just be widened to longs(because widening beats boxing)?
But there is no method which takes two long values. Instead you have a var-arg method taking long. But here the other rule applies that Boxing beats var-arg...
|
 |
Duran Harris
Ranch Hand
Joined: Nov 09, 2008
Posts: 571
|
|
Please explain this: public Spaceman getSpaceman() { return new Object(); } Why is this not allowed??
|
 |
Punit Singh
Ranch Hand
Joined: Oct 16, 2008
Posts: 952
|
|
|
First you should tell us why this should be allowed?
|
SCJP 6
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
(I am here assuming that you have not implemented your custom Object class which extends from Spaceman ) This is not allowed as you cannot convert an Object into a Spaceman. You cannot convert a super class object into a sub-class object directly without a cast...
|
 |
Duran Harris
Ranch Hand
Joined: Nov 09, 2008
Posts: 571
|
|
|
Ah and what about returning a SpaceMan when object is expected?Why isn't that allowed since object is always the superclass?
|
 |
Punit Singh
Ranch Hand
Joined: Oct 16, 2008
Posts: 952
|
|
Dog is an Animal. Cat is an Animal. You can say having closed your eye. But Animal is Dog or Cat or Horse or Cow or Monkey.... oh please stop without seeing them, how can you say. So Animal reference can take Dog/Cat/Horse..... stop please Animal Aref=new Dog(); But Dog reference cannot take Animal ref Animal ArefC=new Cat(); Dog dogRef=ArefC; It is like in Cat's house, you want to put Horse to live
|
 |
Punit Singh
Ranch Hand
Joined: Oct 16, 2008
Posts: 952
|
|
Ah and what about returning a SpaceMan when object is expected?Why isn't that allowed since object is always the superclass?
I think this is allowed Duran.
|
 |
Duran Harris
Ranch Hand
Joined: Nov 09, 2008
Posts: 571
|
|
|
Ok
|
 |
 |
|
|
subject: Widening beats boxing?
|
|
|