• 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
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Applicable methods

 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1)class XYZ {

void method(int i) {} // line (1)

void method(float f) {} // line (2)

public static void main(String[] args) {

new XYZ().method(2); // line (3)
}
}

In line (3) is their a Question of applicable methods.
or is compiler going to select directly method in line (1) as the method call in line (3) matches exactly with method signature in line (1)?

2) class ABC {

void method(String s) {} // line (1)

void method(Object o) {} // line (2)

public static void main(String[] args) {

new ABC().method("ABC"); // line (3)
}
}

Same kind of Question.
In line (3) is their a Question of applicable methods.
or is compiler going to select directly method in line (1) as the method call in line (3) matches exactly with method signature in line (1)?


Thanks in Advance.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In these situations, the "most specific" method is invoked. For example, an argument of type String is considered more specific than Object because a String reference can be upcast to type Object, but not vice versa.

See JLS - 15.12.2.5 Choosing the Most Specific Method.
 
Girish Nagaraj
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks marc
 
So I left, I came home, and I ate some pie. And then I read this tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic