wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes overloaded methods with boxing strange behaviour Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "overloaded methods with boxing strange behaviour" Watch "overloaded methods with boxing strange behaviour" New topic
Author

overloaded methods with boxing strange behaviour

Tanu Gulati
Ranch Hand

Joined: Oct 06, 2008
Posts: 113

i have found one strange behaviour in java. seee below code it compiles and run successfully. and prints "Integer,Integer is called" but once i commented this method i got the error on compilation saying "The method method[] is ambigous for the type Integer[]" this is because we have two method with parameters (Integer... i ) and (int... i ) but they were there earlier also (before commenting out the method (in bold) below then why i was not getting any error earlier and after commenting out the method i got.?? is it not strange.




thanks
Tanu Gulati
Gamini Sirisena
Ranch Hand

Joined: Aug 05, 2008
Posts: 347
Strange indeed! Works fine for me, with either method commented.

Let us know how to reproduce the problem..
Tanu Gulati
Ranch Hand

Joined: Oct 06, 2008
Posts: 113

1.) copy the code and run ..it produces the output.
2.) commment out the method in bold letters given above.
3.) compilation fails now saying "The method method[] is ambigous for the type Integer[]"

are you following the same procedure ?
Gamini Sirisena
Ranch Hand

Joined: Aug 05, 2008
Posts: 347
Ok my mistake.. I saw the last two methods as one because they were cluttered closer.

But I am getting the following error..


javac Test5.java
Test5.java:5: reference to method is ambiguous, both method method(java.lang.Int
eger...) in Test5 and method method(int...) in Test5 match
method(i,k);
^
1 error

The error seems self explanatory. I am not sure why you are getting a different error. Are you using a different JDK than sun?
Tanu Gulati
Ranch Hand

Joined: Oct 06, 2008
Posts: 113

Are you getting the error when code in bold is not commented?

Let me make more clear, see we have three methods 1st and 3rd are ambiguous but i am getting the error only when i comment the second method i am putting it again please have a look.



[ November 28, 2008: Message edited by: Tanu Gulati ]
[ December 28, 2008: Message edited by: Tanu Gulati ]
Gamini Sirisena
Ranch Hand

Joined: Aug 05, 2008
Posts: 347
Tanu,
Looking more closely, it does seem a bit strange.

What is strange here is why the compiler allows you to declare the two methods but will only complain when you call either one. If there is a more obvious method the compiler doesn't seem to have a problem (For example int, int or Integer, Integer probably because they take exactly two parameters).

Apart from this it seems prudent for the compiler to complain that it is not sure which method to invoke. I would have liked the compiler to complain about ambiguities between the int int and Integer Integer versions too.
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

Hi,

As said in KS book,when overloaded the compiler's preference are
1. Widening
2. Boxing
3. Var-args.

So if you apply the above rule you can conclude why the output is "Integer,Integer is called".

When you comment the second method...
Widening is not worth.
So now it can do Boxing and then Var-arg
OR
just do var-arg and call the third method.
So ends up with ambiguity ...

Hope you understood..
[ November 30, 2008: Message edited by: James Tharakan ]

SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
 
GeeCON Prague 2014
 
subject: overloaded methods with boxing strange behaviour