Take the 2-minute tour ×

Just out of curiosity I tried this example.

public class Class1 {

    public void method(Object obj){

    public void method(String str){

    public static void main(String... arg){
        new Class1().method(null);


The output being "String". I want to know on what basis the JVM decides to invoke method taking String as argument and not Object.

share|improve this question

marked as duplicate by NINCOMPOOP, sᴜʀᴇsʜ ᴀᴛᴛᴀ, Uwe Plonus, Soner Gönül, Michael Kjörling Jul 16 '13 at 7:50


Isn't this a compile error? Something about ambiguous parameters? –  Thilo Jul 16 '13 at 6:54
Apparently not. I wish it was. –  Thilo Jul 16 '13 at 6:55
Nothing ambiguous about this, @Thilo. It would be ambiguous if the first method accepted Integer and the second String, since then neither of them would be "more specific" than the other. –  Joni Jul 17 '13 at 13:59
@Joni: Yes, not ambiguous if you know all the exact rules of the spec in detail. But a bit unclear if you don't. It cannot be denied that method selection rules have become quite complex with the introductiong of varargs, generics and autoboxing (of course this particular case has been there since Java 1). I find it weird that Java (which otherwise often leans towards verbose and explicit) is so "flexible" here. Makes for good quiz questions, but not really helpful in everyday's programmer life. –  Thilo Jul 17 '13 at 23:06

7 Answers 7

up vote 7 down vote accepted

Whenever there's Method Overloading, the JVM will search for the method from the most specific type to least specific type

share|improve this answer
I suggest editing your quote from most closest to most specific type would be more appropriate in terms of programming lang. –  exex zian Jul 16 '13 at 6:55
Made correction. Thanks for that. I meant the same, however your words make more sense. –  Reddy Jul 16 '13 at 6:56
It's the compiler that decides which method should be called, not the JVM. –  Joni Jul 16 '13 at 7:59

Whenever more than one overloaded methods can be applied to the argument list, the most specific method is used.

In this case either of the methods can be called when passing null, since the "null type" is assignable to both Object and to String. The method that takes String is more specific so it will be picked.

share|improve this answer

See the JLS specification Choosing the Most Specific Method

It is one of the puzzle of Java Puzzlers by Joshua Bloch - Puzzle 46: Case of the Confusing Constructor

share|improve this answer

Java Compiler chooses the most specific method.

String is a more specific type compared to the Object.

share|improve this answer

Becaue you are passing null in calling method and you defined void method(String str) And String always initlize with null. it will find that matching parametrized method. Thats y u got "str" on console.

share|improve this answer

When you are doing method overloading, jvm tries to the next in hierarchy. For e.g. if you overload methods with long and int, but invoke method by passing byte, it will first go to int as it is next in hierarchy to byte.

share|improve this answer

It's because of method Overloading

The most specific method is chosen at compile time.

As 'java.lang.String' is a more specific type than 'java.lang.Object'. In your case it returns String.

share|improve this answer
