aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Widening and boxing doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Widening and boxing doubt" Watch "Widening and boxing doubt" New topic
Author

Widening and boxing doubt

Kedar Pethe
Ranch Hand

Joined: Jul 17, 2012
Posts: 39
K&B question of self test-

Correct output:- 212

How come doStuff(x) doesn't invoke doStuff(Integer... i) ?? Because we first box int x to an Integer wrapper, right?

If primitives don't extend Object class, then how come int x maps to reference 'o' of doStuff(Object o)??
Pritish Chakraborty
Ranch Hand

Joined: Jun 12, 2012
Posts: 91

You should read the Overloading with Boxing, Widening and Varargs section.

The compiler will indeed box the integer first, seeing that there is no most-specific method which takes a primitive int.

Now the compiler has the option of widening to Object (Integer extends Object via Number) or using the varargs method.

Widening beats varargs because pre-existing code should function as it used to.


OCJP 6
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30764
    
156

doStuff(x, y); - y is a Boolean. Which isn't an Integer, so Integer... can't possibly be called
doStuff(x); - there is only one parameter here making Object a closer match.
doStuff(sa, sa); - short[] isn't an Integer either, so Integer... can't possibly be called

Note that if you comment out the first method, the output is 232. So it's not that doStuff(x) can't call Integer... It's that it chooses not to since Object is present.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Kedar Pethe
Ranch Hand

Joined: Jul 17, 2012
Posts: 39
Pritish Chakraborty wrote:
Now the compiler has the option of widening to Object (Integer extends Object via Number) or using the varargs method.


Im confused..
Boxing means eg- int -> Integer.
widening means eg- int -> long,
please explain how did you say that compiler has the option of widening to Object.
How can you widen int to an Object??

Kedar Pethe
Ranch Hand

Joined: Jul 17, 2012
Posts: 39
Jeanne Boyarsky wrote:doStuff(x, y); - y is a Boolean. Which isn't an Integer, so Integer... can't possibly be called
doStuff(x); - there is only one parameter here making Object a closer match.
doStuff(sa, sa); - short[] isn't an Integer either, so Integer... can't possibly be called

Note that if you comment out the first method, the output is 232. So it's not that doStuff(x) can't call Integer... It's that it chooses not to since Object is present.


I am not getting how can a primitive map to an Object, as Object is a superclass and Integer is a subclass, isn't int closer to java.lang.Integer ??
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30764
    
156

Yes. int boxes to Integer. And Integer is an Object because every class extends Object indirectly.
Kedar Pethe
Ranch Hand

Joined: Jul 17, 2012
Posts: 39
Got it. Thanks Jeanne & Pritish for the explaination!!
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

i think you are confused between widening reference and widening primitive conversions. these are two seperate things. if Dog class subclass Animal and we write Dog d = new Dog(); Animal a = d; here widening reference conversion is happening. when we do long l = 989; here widening primitive conversion is happening. int is promoted to long implicitly. so and int can be promoted to Object in your example. first it is boxed to Integer wrapper which is then assigned to Object reference variable and hence widening reference conversion. Rest is explained by Pritish
Kedar Pethe
Ranch Hand

Joined: Jul 17, 2012
Posts: 39
gurpeet singh wrote:i think you are confused between widening reference and widening primitive conversions. these are two seperate things. if Dog class subclass Animal and we write Dog d = new Dog(); Animal a = d; here widening reference conversion is happening. when we do long l = 989; here widening primitive conversion is happening. int is promoted to long implicitly. so and int can be promoted to Object in your example. first it is boxed to Integer wrapper which is then assigned to Object reference variable and hence widening reference conversion. Rest is explained by Pritish


Thanks!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Widening and boxing doubt