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
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Widening beats boxing?" Watch "Widening beats boxing?" New topic
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
    
    2

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
    
    2

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
    
    2

(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
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Widening beats boxing?
 
Similar Threads
doubt in double and Integer
Method calling
Widening, Autoboxing VarArgs method Choice made by the JVM
KS&BB SCJP Study Guide Self Test Chap 2 Question 15
Why doest it print it?