Rectangle 27 1

java Does this function call itself or call the overload?


Double d = getSomeDoubleValueFromAnywhere();
add(d);
Double dv = null;
add(value == null ? dv : value.doubleValue());
lub
lub(null,Double)
  • That the method argument is a conditional expression
  • That the method is being called from an overload

... then the conditional expression type would be double, and you'd get a NullPointerException if value were ever null, because it would be trying to unbox the null value.

... which method would you expect to be called? Presumably the add(Double) method - so that's what is called in your situation too.

@FitzwilliamBennet-Darcy: My mistake - it wasn't a bug! (There are similar situations where it is though... I'm trying to remember the exact case now...)

@MadPhysicist: I'm still working on this, as it's not clear whether the type of the conditional expression is actually double or Double.

Basically, there are two parts of this that are irrelevant:

So if you think about it as:

Thank you very much for the advice, and for fixing one of my very many bugs.

The tricky part is working out the type of the conditional expression - is it Double or double? I believe the rules (which are hard to follow, IMO) mean that it's Double, due to the use of a null literal (which is of the null type). If instead you had:

Yes, it's well defined that it will call the Double overload. It couldn't call the Integer overload because there's no implicit conversion from double (which is the type of the conditional expression) to Integer.

Note
Rectangle 27 1

java Does this function call itself or call the overload?


Double d = getSomeDoubleValueFromAnywhere();
add(d);
Double dv = null;
add(value == null ? dv : value.doubleValue());
lub
lub(null,Double)
  • That the method argument is a conditional expression
  • That the method is being called from an overload

... then the conditional expression type would be double, and you'd get a NullPointerException if value were ever null, because it would be trying to unbox the null value.

... which method would you expect to be called? Presumably the add(Double) method - so that's what is called in your situation too.

@FitzwilliamBennet-Darcy: My mistake - it wasn't a bug! (There are similar situations where it is though... I'm trying to remember the exact case now...)

@MadPhysicist: I'm still working on this, as it's not clear whether the type of the conditional expression is actually double or Double.

Basically, there are two parts of this that are irrelevant:

So if you think about it as:

Thank you very much for the advice, and for fixing one of my very many bugs.

The tricky part is working out the type of the conditional expression - is it Double or double? I believe the rules (which are hard to follow, IMO) mean that it's Double, due to the use of a null literal (which is of the null type). If instead you had:

Yes, it's well defined that it will call the Double overload. It couldn't call the Integer overload because there's no implicit conversion from double (which is the type of the conditional expression) to Integer.

Note