Question d’entretien chez MakeMyTrip

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

Réponses aux questions d'entretien

Utilisateur anonyme

12 juill. 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

Utilisateur anonyme

27 oct. 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.

Utilisateur anonyme

20 août 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.