• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Boxing Doubt

 
Sanjeev Narula
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Boxing9 {
public static void main(String[] args) {
int i = 10;
method(i);
}
static void method(Object o){
System.out.println("Object called");
}
static void method(Number n){
System.out.println("Number called");
}
}


what will be answer, after boxing of primitive 'i' to Integer i. it should get widen. Since Integer is a Number and also Integer is a Object.

Which method will be called?
 
Wiyanto Ngasinur
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think the method will be called is the most specific ones.
since i is primitive int, it will be boxing to Integer... then it will called method which parameter is Number, since is the more specific than Object.
 
sadasiva kumar
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since Integer is a Number and also Integer is a Object.

Which method will be called?


Here you are passing Integer value 10 and you are not passing Object reference value.So automatically it calls the method which has Number as parameter.
 
Scott Tiger
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i also think it will invoke method(Number n) this method
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic