• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

boxing

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class A{
int doX(Long x,Long y){
return 1;
}
int doX(long...x){
return 2;
}
int doX(Integer x,Integer y){
return 3;
}
int doX(Number n,Number m){
return 4;
}
public static void main(String ar[]){
new A().go();
}
void go(){
short s = 7;
System.out.println(doX(s,s)+"");
System.out.println(doX(7,7));
}
}

short-->long-->Long //widing & boxing
short-->long...x //widing & boxing
short-->Short-->Integer//boxing & widing this is wrong
short-->Short-->Number //boxing & widing

int-->long--Long //widing & boxing
int-->long...x //widing & boxing
int-->Integer //boxing & widing //this is also correct
int-->Integer-->Number //boxing & widing // this is also correct


so how come the answer is 4,3
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
The answer 3 is right because
First it search for
1)primitive type then
2)Wapper classes then
3)varargs.
but i don't have idea for the O/P 4 and (on Number class)
 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the first invocation you pass two short primitives, first they are boxed to Short wrappers, now it has to find a match so it widen to Number as the only choice, here you cant widen to other type IS-A fails, Short cant become Integer.
In the second invocation you pass two int primitives which are simply boxed to Integers.
 
So it takes a day for light to pass through this glass? So this was yesterday's tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic