This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes boxing method call 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 method call doubt" Watch "boxing method call doubt" New topic

boxing method call doubt

dolly shah
Ranch Hand

Joined: Jun 18, 2007
Posts: 383

What will be the output for the above program?
1)Object called
2)Number called-answer
3)Compiler Error
4)Runtim Exception

Why the answer is Number? I thought it is ambiguous between Object & Number. So the answer is compiler error.

SCJP-1.5<br />SCWCD-1.4
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
No, it's not ambiguous.

The call matches both methods, but the one with parameter Number is more specific than the one with parameter Object. The most specific method that matches will be chosen in this case.
dolly shah
Ranch Hand

Joined: Jun 18, 2007
Posts: 383
Thanks. I just forgot.
Yogvinder Singh Ghotra
Ranch Hand

Joined: Sep 10, 2007
Posts: 38
Can someone explain this to me in detail??
ahmed yehia
Ranch Hand

Joined: Apr 22, 2006
Posts: 424
Here the int literal 10 is first boxed to Integer which is then widened to Number.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15041

When two methods match the call, Java chooses the most specific method. See section of the Java Language Specification. In this case, the method that takes a Number is more specific than the method that takes an Object.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
I agree. Here's the link:
subject: boxing method call doubt
jQuery in Action, 3rd edition