File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
Author

Boxing/conversion question. Thanks.

Sihao Rui
Greenhorn

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;
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

Joined: Oct 07, 2007
Posts: 33
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Boxing/conversion question. Thanks.