String concatenation does not work properly in Java when concatenating 2 results of ternary operators

前端 未结 4 1444
无人及你
无人及你 2021-01-03 05:05

Dear Java guru \'s!

Can you, please, explain me, why String concatenation does not work properly in Java when concatenating 2 results of ternary operators?

4条回答
  •  时光取名叫无心
    2021-01-03 05:35

    The problem is probably the order of operations. You can make it explicit by writing:

    String x = (str != null ? "A" : "B") + (str == null ? "C" : "D");
    

提交回复
热议问题