Is there a conditional operator without the else part in Java?
conditional-operator, java
Solution
It's an expression, not a statement, which means it always evaluates to something. If there wasn't an "else part" there would be nothing for the expression to evaluate to if the test was false. So, no, there's nothing similar without the else.
The thing I like about using the conditional operator is that you can assign something like
foo = a > b ? c : d;
and reading it you know foo got something assigned to it, regardless of whether the test was true. So it provides you with a way to indicate that a value is assigned that depends on some test.
Groovy has some similar operators:
`?:`, the Elvis operator, assigns the value on the right as a default if the expression on the left is null.
`?.`, the safe-null operator, evaluates to null if the left side evaluates to null. This is handy for cases where you have a chain of possibly-null things, like `foo?.bar?.baz`, where you would rather not blow up with an NPE if something is null but would rather not type out all the null checks.
Problem
I know that that you can use something like this in Java: ``` (a > b) ? a : b; ``` Is there something similar just without the else part?