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

前端 未结 4 1450
无人及你
无人及你 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:43

    I think you intended

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

提交回复
热议问题