Domanda di colloquio di MakeMyTrip

void sort(String s){ } void sort(Integer I){ } void sort(Object o){ } calling : sort(null) which function will be called?

Risposte di colloquio

Anonimo

12 lug 2016

Void Sort(String s) will be called , the reason is in java at run time method overloading goes with the most specific case and as we know null is an instance of string having length as 0.

1

Anonimo

27 ott 2018

It will show compiler error as it will be confused to go with Integer or String. But if have only Integer, or String or Object or String and Object both or Integer and Object both, it will compile successfully and will go to Integer or String method resp.

Anonimo

20 ago 2016

It'll fail with compile error. Its an ambiguous method call. because, both sort(String) and sort(Integer) will match 'null'. FYI, null is NOT an instance of String.