Which overload will get selected for null in Java?

后端 未结 3 792
没有蜡笔的小新
没有蜡笔的小新 2020-11-22 10:42

If I write this line in Java:

JOptionPane.showInputDialog(null, \"Write something\");

Which method will be called?

  • show
3条回答
  •  余生分开走
    2020-11-22 11:10

    The most specific method will be called - in this case

    showInputDialog(Component parent, Object message)
    

    This generally comes under the "Determine Method Signature" step of overload resolution in the spec (15.12.2), and in particular "Choosing the Most Specific Method".

    Without getting into the details (which you can read just as well in the spec as here), the introduction gives a good summary:

    If more than one member method is both accessible and applicable to a method invocation, it is necessary to choose one to provide the descriptor for the run-time method dispatch. The Java programming language uses the rule that the most specific method is chosen.

    The informal intuition is that one method is more specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time type error.

提交回复
热议问题