I embedded a ternary operator in string as following:
return borderStyle.getThickness() + "|" +
borderStyle.getColor()!=null?ColorPersistor.asString(borderStyle.getColor()):"isnull" + "|" +
borderStyle.getLineStyle();
Surprisingly ColorPersistor.asString(borderStyle.getColor())
was executed.
Why wasnt the call evaluated to "isnull" Isn't it possible to embed ternary operator in strings?
The ternary operator has one of the lowest Operator Precedeces.
That means: It is evaluated after the + operations in front of that.
So in fact you're not checking if borderStyle.getColor()
is not null
but you check that
borderStyle.getThickness() + "|" + borderStyle.getColor()
is not null. Which is isn't as the literal String "|"
is always != null
.
Whenever you use the ternary operator, be sure to use ( and ) to make sure you apply to the correct part of your equation.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments