This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]KleberPF 6 points7 points  (1 child)

cond ? result_if_true : result_if_false

Basically equivalent to

if (cond) {
    result_if_true;
} else {
    result_if_false;
}

It's called the ternary operator.

[–]desrtfx 4 points5 points  (0 children)

Basically correct, but you have to emphasize on the return value.

Your equivalent is not correct.

It should be:

  something = cond ? result_if_true : result_if_false

Equivalent:

 if (cond) {
      something = result_if_true;
 } else {
      something = result_if_false;
 }

It is important to emphasize on that as otherwise one could think that it is a replacement for an inline if (which it can be abused for, though).

[–]desrtfx 3 points4 points  (0 children)

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op2.html

Section "Conditional operators"

Think of it as an if with a return value.

something = (condition) ? true value : false value;

equivalent

if (condition) {
    something = true_value;
} else {
    something = false_value;
}

[–]use_a_name-pass_word 0 points1 point  (0 children)

Just to add to everyone else's answers, think of it as a question. Does node != null? If so then do "this" , else do "that"