• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Method Overloading(var args,wrapper,premitive)

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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");
}
 
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
abhinas raj
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 don't like that guy. The tiny ad agrees with me.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic