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 Var-args and method overloading doubt 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 "Var-args and method overloading doubt" Watch "Var-args and method overloading doubt" New topic

Var-args and method overloading doubt

Sridhar Gudipalli
Ranch Hand

Joined: Nov 02, 2005
Posts: 120
Source: Exam Lab Practice test #2, Q#33

This program allows to overload the methods in this way. But doesnot allow to call them. I understand the reason of ambiguity. But if that is the case why the compiler allows to define these methods? In other way, how to invoke these methods? Am I missing something here? Any help please!!

Sridhar Gudipalli|SCJP 6.0
SCWCD objectives
Joshua Antony
Ranch Hand

Joined: Jun 05, 2006
Posts: 254

Above code gets executed successfully

Emanuele Ghe
Ranch Hand

Joined: Feb 04, 2009
Posts: 111
Joshua Antony wrote:

Above code gets executed successfully

I have the same doubt and I think it works because widening is preferred above boxing, so it wides the bytes to int and calls
the int one.

If you try to pass any int/integer, it does not work.

Why ?

SCJP6 with score 90%. I am conscious of my ignorance and ready to learn from everyone.
Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316
NOTE: Compiler does not perform widening and Autoboxing (in this order). It is too much for a compiler to perform. In a Nutshell, compiler does not try to Autobox after widening.

In case of callMethod(2,3) : Bot the arguments are int , hence compiler would get confused, because both could be widened or Autoboxed.
When Widened:- Matched to callMethod(int... i)
When Autoboxed:- Matched to callMethod(Integer, Integer);

In case of callMethod((byte)2,(byte)3) : Both the arguments are byte. Compiler looks for callMethod(byte, byte). After failure it can match it for callMethod(int...i) by "Widening" bytes. Since after widening compiler never performs Autooboxing therefore this time compiler is not confused about linking the call by actual method, since this call only matches with callMethod(int...i).

Hope you understand !!!
Take care

SCJP 6.0 96%

(Connecting the Dots ....)
Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316
Compiler allows to define the above methods because they can actually be called without ambiguity.

callMethod((byte)2,(byte)3); -->Calls callMehtod(int...i) without Ambiguity.

<Dont know > -->Calls callMethod(Integer,Integer).

Can anyone tell which call wuld invoke callMethod(Integer...i) without Ambiguity !!!

Thanks !!!
suresh krishna Avanthkker

Joined: May 24, 2010
Posts: 14
"widening should not lose out to newly created method that relies on boxing"as quoted in book.
if argument is byte and in the parameters there is an option of and then callMethod(int, int) will work.

"you can box and widen(An int can become an Object , through an Integer" you cant do the reverse.
In callMethod(2, 3) since both 2 and 3 is byte, I think callMethod(int ...i) will not be called as widening
beats boxing and boxing beats var-args .AscallMethod(int ...i) is var-args ,widening will beat var-arg and there
no option of widening i.e callMethod(int, int) therefore then boxing has to take place
so byte is boxed to Byte and widened to Object and callMethod(Integer... i) will cast the Object reference
to Integer, therefore callMethod(Integer... i) has to work.

I agree. Here's the link:
subject: Var-args and method overloading doubt
It's not a secret anymore!