File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Widening and boxing doubt

 
Kedar Pethe
Ranch Hand
Posts: 39
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 91
C++ Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 33691
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kedar Pethe
Ranch Hand
Posts: 39
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33691
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. int boxes to Integer. And Integer is an Object because every class extends Object indirectly.
 
Kedar Pethe
Ranch Hand
Posts: 39
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it. Thanks Jeanne & Pritish for the explaination!!
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic