Meaningless Drivel is fun!*
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


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Boxing/conversion question. Thanks.