• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Widening and boxing doubt

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)??
 
Ranch Hand
Posts: 91
Firefox Browser C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author & internet detective
Posts: 42003
911
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 42003
911
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it. Thanks Jeanne & Pritish for the explaination!!
 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!
 
It runs on an internal combustion engine. This ad does not:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic