• 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

why does it behave like that?

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got a piece of code from one of the mock exams, which I can't explain. Who can tell me why it compiles, and prints "String Version"?
public class StringTest{
public void method(Object o) {
System.out.println("Object Version");
}
public void method(String s) {
System.out.println("String Version");
}
public static void main(String args[]) {
StringTest test = new StringTest();
test.method(null);
}
}
Thank you very much.
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bogdan
What happens in a method call is that the most specific method of the appropriate class is the one that is called. In very general terms in the code you posted this wou,d be the String argument method because it is lower in the heirarchy than the Object argument method.
Here is the actual quote form the JLS section 15.12.2.2


If more than one method declaration is both accessible and applicable to a method invocation, it is necessary to choose one to provide the descriptor for the run-time method dispatch. The Java programming language uses the rule that the most specific method is chosen.
The informal intuition is that one method declaration is more specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time type error.


Basically, becasue you can pass a String as an argument to a method that is looking for an Object, the String method is the most specific.

Hope that clears it up for you

------------------
Dave
Sun Certified Programmer for the Java� 2 Platform
[This message has been edited by Thomas Paul (edited August 31, 2001).]
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We have had this discussion before:
http://www.javaranch.com/ubb/Forum24/HTML/011295.html
------------------
Tom - SCJP --- Co-Moderator of the Programmer Certification Forums
 
Bogdan Sheptunov
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, I see now.
 
reply
    Bookmark Topic Watch Topic
  • New Topic