File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Widening beats boxing? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Widening beats boxing?" Watch "Widening beats boxing?" New topic
Author

Widening beats boxing?

Duran Harris
Ranch Hand

Joined: Nov 09, 2008
Posts: 598

Isn't it true that widening beats boxing?


===>SCJP 1.5(72%)<===
==>SCWCD1.5(76%)<===
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

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: 598

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: 598

Aaah damn...I thought they were longs..not Longs.
OK problem solved
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

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: 598

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
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

(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: 598

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: 598

Ok
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Widening beats boxing?