File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Method Overloading(var args,wrapper,premitive) 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 "Method Overloading(var args,wrapper,premitive)" Watch "Method Overloading(var args,wrapper,premitive)" New topic
Author

Method Overloading(var args,wrapper,premitive)

abhinas raj
Ranch Hand

Joined: Jun 02, 2012
Posts: 47
in case A) when the method callMethod() is invoked by passing an integer argument the premitive [ callMethod(int i) ], is called ,that is priority is given to premiotive thats ok

but in case B) when variable arguments is used then it shows ambiguity error why ?
why priority is not given to premitive type here ?

A)
public static void callMethod(Integer i){
System.out.println("Wrapper");
}

public static void callMethod(int i){
System.out.println("Primitive");
}
-----------------------------------------------------------------------------------------------------------
B)

public static void callMethod(Integer... i){
System.out.println("Wrapper");
}

public static void callMethod(int... i){
System.out.println("Primitive");
}
Kevin Florish
Ranch Hand

Joined: Jan 06, 2009
Posts: 176
Without the actual input to the method and the error log I would say this is because of Boxing by the compiler.

So when the int var-args is boxed you have an ambiguity.


Java5 Tutorials | Java6 Tutorials | HTML/CSS Tutorials | JavaScript and jQuery Tutorials
abhinas raj
Ranch Hand

Joined: Jun 02, 2012
Posts: 47
sorry i couldnt understand your answer , would you please elaborate it

what is meaning of Without the actual input to the method and the error log ?

and when int var-args is boxed you have an ambiguity. why int var-args will be boxed , it has to be just invoked na, what is need od boxing it ?
Rameshwar Soni
Ranch Hand

Joined: Feb 03, 2011
Posts: 247
Read again what Kevin said, this time slowly.

Your question is incomplete. You haven't shown what input or how you are invoking the method. So show the code which you haven't understood and the way (in your case) you are invoking the methods and plus the error which compiler gives.
abhinas raj
Ranch Hand

Joined: Jun 02, 2012
Posts: 47
as i wrote in my question that when the method callMethod() is invoked by passing an integer argument--- that means object.callMethod(4); i am writting the whole code for better understanding.

class anything{
public static void main (String args[]){

Rameshwar object=new Rameshwar();

// invoking the methd by passing 4 as argument
object.callMethod(4); //

}
}


class Rameshwar {


// condition A)

public static void callMethod(Integer i){ // 1.
System.out.println("Wrapper");
}

public static void callMethod(int i){ // 2.
System.out.println("Primitive");
}

// both the above methods are Overloaded methods. when we pass 4 as argument to the method then 2. method is invoked b\c priority has is given to int(premitive) over
Integer(wrapper) that is ok. now i want to know that why this priority rule is not applied for following condition ...... B)



// -----------------------------------------------------------------------------------------------------------
//condition B)

public static void callMethod(Integer... i){
System.out.println("Wrapper");
}

public static void callMethod(int... i){
System.out.println("Primitive");
}

}


in the above condition B) there are two Overloaded methods with parameter as variable arguments. in this case also i am passing 4 as parameter (we can pass more than arguments also as it is var args but i am passing here 4 only), then compiler show error that

-------------- > reference to both the methods is ambigous

i want to know that why it is giving error ?
since both methods contains var args (i.e common on both) then why here the methods are not invoked on the basis of priority(giving more priority to int... than Integer...)
Kevin Florish
Ranch Hand

Joined: Jan 06, 2009
Posts: 176
Abhinas please put Code tags around your code as it makes it easier to read and takes up a lot less room.

Thanks Kevin.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Method Overloading(var args,wrapper,premitive)