• Post Reply Bookmark Topic Watch Topic
  • New Topic

How are method arguments resolved ?  RSS feed

 
Faisal syed
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
package corejava;

public class ArgumentResolver {

public static void method(String s){
System.out.println("string");

}

public static void method(Object o){
System.out.println("object");

}
public static void main(String[] args) {
method(null);

}

}


--
output is string. Please explain this
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37496
547
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's complicated. In this case, String is more specific than Object so it is chosen.

But if you were passing null and had these two methods available, you'd get a compiler error because Java wouldn't know what to do:

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!