This is puzzling me and I would like an explanation.
public foo(EventPoint... eventPoints) {
//...
}
boolean isThisHappening;
foo(isThisHappening ? new EventPoint() : new EventPoint[]{});
Even though foo(new EventPoint()); is valid and foo(new EventPoint[]{}); is also valid. Is this the ternary operator failing due to type evaluation?
Using JDK 1.7.0
Getting the error:
required: EventPoint[]
found: Object
reason: argument type Object does not conform to vararg element type EventPoint
The type of a ternary expression is basically the most specific subtype of the two last operands (JLS Reference). The most specific subtype of
EventPointandEventPoint[]isObject. If your var-arg method is declared to acceptEventPoint...it simply won’t work.You should change
to
and it should compile just fine.