null
can be converted to Object
or String
, but not int
. Therefore the second constructor is out.
Between the conversion to Object
or the conversion to String
, the conversion to String
is more specific, so that's what's picked.
The JLS section 15.12.2 describes method overload resolution, and I believe the same approach is used for constructor resolution. Section 15.12.2.5 describes choosing the most specific method (constructor in this case):
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.
This about the constructor invocation with Object or String arguments - any invocation handled by new Demo(String)
could also be passed on to new Demo(Object)
without a compile-time type error, but the reverse is not true, therefore the new Demo(String)
one is more specific... and thus chosen by the overload resolution rules.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…