Java ternary operator (?:) doesn't work; second or third operand return boolean

后端 未结 2 616
别那么骄傲
别那么骄傲 2020-12-04 03:08

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 d         


        
2条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-04 03:52

    Usage of Java ternary operation condition should looks like

    result = testCondition ? value1 : value2
    

    it's java-language specification.

    Equality, Relational, and Conditional Operators

    In the following example, this operator should be read as: "If someCondition is true, assign the value of value1 to result. Otherwise, assign the value of value2 to result

提交回复
热议问题