Meaningless Drivel is fun!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Boxing Doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Boxing Doubt" Watch "Boxing Doubt" New topic

Boxing Doubt

Sanjeev Narula

Joined: Mar 16, 2007
Posts: 19
public class Boxing9 {
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 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

Joined: Dec 11, 2006
Posts: 34
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

Joined: Oct 30, 2003
Posts: 91
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.

A bend in the road is not the end of the road ... unless you fail to make the turn.
Scott Tiger

Joined: Mar 23, 2007
Posts: 7
i also think it will invoke method(Number n) this method
I agree. Here's the link:
subject: Boxing Doubt
It's not a secret anymore!