• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

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.
 
If you have a bad day in October, have a slice of banana cream pie. And this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic