java代码中的问号

Q L*_*Liu 0 java operators

有人可以解释下面代码中的问号吗?此外,INITIAL_PERMANCE是代码中的静态最终常量,但synatax的最后一行是什么?

Synapse(AbstractCell inputSource, float permanence) {
    _inputSource = inputSource;
    _permanence = permanence==0.0 ? 
        INITIAL_PERMANENCE : (float)Math.min(1.0,permanence);
}
Run Code Online (Sandbox Code Playgroud)

EdC*_*EdC 9

的?和:是java条件运算符的一部分.有时称为三元运算符,因为它是Java中唯一带有3个参数的运算符.

这本质上是一个内联IF/THEN/ELSE块.

_permanence = permanence==0.0 ? 
    INITIAL_PERMANENCE : (float)Math.min(1.0,permanence);
Run Code Online (Sandbox Code Playgroud)

可以改写如下:

if (permanence == 0.0)
    _permanence = INITIAL_PERMANENCE;
else
    _permanence = (float) Math.min(1.0,permanence);
Run Code Online (Sandbox Code Playgroud)

条件运算符的一般形式是

<Test returning a boolean> ? <value for if test is true> : <value for if test is false>
Run Code Online (Sandbox Code Playgroud)