• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Boxing/conversion question. Thanks.

 
Sihao Rui
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, could anyone help me explain the following one? Which boxing/widen rule I should use? Thanks!

public class Boxing{
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 the output for the above program?
1)Object called
2)Number called
3)Compiler Error
4)Runtim Exception

Answer: (2)
 
Yogesh Baraskar
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

First i is boxed to Integer wrapper object.
Then we need to widen it to the closest Parent class which
in this case is Number.



Regards,
Yoges
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic