• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Explain this code?

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Explain this code?It is getting an error.I do not understand the error.When dostuff is invoked with integer then compiler chose the exact method.If not found it will check for method that takes wider than argument.Then it will choose the boxing and then priority goes to varargs method.Here varargs method with int is there.Why compiler do not chose this method?
class Boxing2
{
public static void main(String args[])
{
dostuff(4);
}
static void dostuff(int... z)
{
System.out.println("int");
}
static void dostuff(Integer... z){
System.out.println("Integer");
}
}
 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here the compiler will look for the most specific method. check out this link http://radio.javaranch.com/corey/2004/08/19/1092931618000.html

basically what happens is that compilers get confused when deciding about which is most-specific method. both the methods can take each others arguments which result in compiler confusion and hence it gives error.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
exam point of view, you may required to understand this

unfortunately, var arg method overloaded is mess. generally as a thumb rule never overload a var-arg method
 
vvus bharadwaj
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I studied the most specific method tutorial.I understand that when there are two or more applicable methods are there then the compiler will choose the specific method.That method can be chosen by comparing the applicable method arguments.The method1 arguments that are valid for (i.e they can be assigned to)method2 arguments but not vice versa then method1 is most specific.In the first example i posted method1 arguments(int) can be easily assigned to method arguments(Integer) and method2(Integer) arguments are also easily assigned to method1(int). So compiler cannot determine the specific method.
This is the first example.
class Boxing2
{
public static void main(String args[])
{
dostuff(4);
}
static void dostuff(int... z) //method1
{
System.out.println("int");
}
static void dostuff(Integer... z) //method2
{
System.out.println("Integer");
}
}
I understood this much concept from what i have studied.Is this right?Then i have another doubt regarding the below example?
class Boxing2
{
public static void main(String args[])
{
dostuff(4);
}
static void dostuff(long... z) //method1
{
System.out.println("int");
}
static void dostuff(Integer... z) //method2
{
System.out.println("Integer");
}
}
In this example method1 arguments(long) cannot be assigned to method2 arguments(Integer) but method2 arguments(Integer) can be assigned to method1 arguments(long).So method2 is most specific methos.Why compiler cannot determine this one and showing an error that is same error as in first example?
 
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Integer and int were same thing,how do you think that long and Integer are same ?
 
gurpeet singh
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i would when i read that article regarding most specific method on corey's timeline i was not at all much satisfied with what he said regarding how to determine most specific method. i would wait for experts to answer this question and also to shed light on most specific method. how to determine whether one method is most specific method or not ? all i can say is that from the compiler error we can deduce that definitely it is the case of ambiguity and compiler is not able to determine the most specific method.
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic