• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Widening beats boxing?

 
Duran Harris
Ranch Hand
Posts: 608
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Isn't it true that widening beats boxing?
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes it is true. This is done to maintain the behavior of legacy code when there was no auto boxing...
 
Himanshu Gupta
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
web page This will help you a lot.
 
Duran Harris
Ranch Hand
Posts: 608
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 608
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aaah damn...I thought they were longs..not Longs.
OK problem solved
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 608
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please explain this:

public Spaceman getSpaceman()
{

return new Object();

}

Why is this not allowed??
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First you should tell us why this should be allowed?
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(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
Posts: 608
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 608
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic