File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Boxing/conversion question. Thanks. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Boxing/conversion question. Thanks. " Watch "Boxing/conversion question. Thanks. " New topic

Boxing/conversion question. Thanks.

Sihao Rui

Joined: Oct 14, 2007
Posts: 10
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;
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

Joined: Oct 07, 2007
Posts: 33

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.

I agree. Here's the link:
subject: Boxing/conversion question. Thanks.
It's not a secret anymore!