wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes overloading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "overloading " Watch "overloading " New topic
Author

overloading

sweety sinha
Ranch Hand

Joined: Jul 07, 2008
Posts: 76
class GFC303{
public static void main(String[] args)
{
GFC303 inst_test = new GFC303();
inst_test.method (1, 1, 1);
inst_test.method( new Integer(1) , new Integer(2) , new Integer(3) );
inst_test.method ( 1 , new Integer(5) );
inst_test.method ( new Integer(10) , 1 );
}

public void method( Integer... I )
{
System.out.println("Eye in the sky");
}

public void method( int... i )
{
System.out.println("Fly in the pie");
}

}
this code is giving compilation error:- reference to method is ambiguous
But when changed as

GFC303 inst_test = new GFC303();
inst_test.method (1);
inst_test.method( new Integer(1) );
inst_test.method ( 1 );
inst_test.method ( new Integer(10) );
}

public void method( Integer I )
{
System.out.println("Eye in the sky");
}

public void method( int i )
{
System.out.println("Fly in the pie");
}
it is compiling fine and giving the desired result
so what is happening in the 1st code?

thanks in advance
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17

Well I thought that out of the four calls
inst_test.method (1, 1, 1);
inst_test.method( new Integer(1) , new Integer(2) , new Integer(3) );
inst_test.method ( 1 , new Integer(5) );
inst_test.method ( new Integer(10) , 1 );

last two will generate a compile time error but all of them are generating a compile time error.

This is happening because the compiler get's confused while creating an array for the parameters. As you might know, that var-args are converted into arrays. So method(int... i) will become method(int[] i) and method(Integer... i) will become method(Integer[] i).

So when you pass method(1,1,1) the compiler tries to convert it into an array. But it get's confused whether to box all the elements then create an array
i.e. method(new Integer[]{new Integer(1),new Integer(1),new Integer(1)})

or to create an array directly
i.e. method(new int[]{1,1,1})

I don't know why this is happening as the compiler should just create an array without wrapping any values, but I think that this thing is not defined as yet(i.e. it is not decided whether boxing should be done or not) that is why the compiler says that both methods match the call.

In the second call the compiler can't decide whether to call the method directly or un-box the values and then call the method...
Same is the case with the other calls....The compiler can't decide whether to box or un-box...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
sannuth kashikar
Greenhorn

Joined: Sep 16, 2008
Posts: 14
All the 4 method calls will fail since the compiler cannot determine which version of the method to be called, to box or unbox the arguments passed to it. Integer is object version of int.The operations performed on int can also performed on Integers.
so,
inst_test.method (1, 1, 1); // to box or is of type int

inst_test.method( new Integer(1) , new Integer(2) , new Integer(3) );
// to unbox or is of type Integer

inst_test.method ( 1 , new Integer(5) );//wheather to box or unbox

inst_test.method ( new Integer(10) , 1 );//wheather to box or unbox

Hence compiler error.


scjp5 90%
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: overloading