Can someone tell me why this use of the ternary operator is incorrect? Operands 2 and 3 return a boolean.
public class Something {
...
private static final double REFERENCE_FRAME_MID_X = 0;
private static final double REFERENCE_FRAME_MID_Y = 0;
private boolean findInsideOrOutsideGeneralEllipse(Point2D destCirclePos) {
List<Boolean> returnValue = new ArrayList<>();
Point2D referenceFrameCenter = new Point2D.Double(REFERENCE_FRAME_MID_X, REFERENCE_FRAME_MID_Y);
Ellipse2D insideAreaEllipse2D = getEllipse2D(referenceFrameCenter.getX(), referenceFrameCenter.getY(),
destCirclePos.distance(referenceFrameCenter));
// doesn't work
insideAreaEllipse2D.contains(destCirclePos) ? returnValue.add(true) : returnValue.add(false);
// works
if (insideAreaEllipse2D.contains(destCirclePos)) {
returnValue.add(true);
} else {
returnValue.add(false);
}
}
...
}
Usage of Java ternary operation condition should looks like
it's java-language specification.
Equality, Relational, and Conditional Operators
From JLS - Conditional Operator:
Grammar of expression statements from JLS - 14.8:
Now the way you are using the conditional operator is not a valid expression statement, as inferred from it's grammar. And hence you get the compiler error. You have to use it in any of the above mentioned context.