0

I think this code should give Error. But it gives me Hello as Output.

public class Main
{
    public static void main(String[] args) {
        foo(null);
    }

    public static void foo(String str){
        System.out.println("Hello");
   }

    public static void foo(Object o){
       System.out.println("Hi");
   }
}

1 Answers1

3

String is a nullable type, therefore your call to foo(String) can take place.

Additionally, the overload resolution rule states that the "most specific" type takes the precedence, hence the string overload is chosen.

Yennefer
  • 5,704
  • 7
  • 31
  • 44