[SalesForce] A special ternary condition in apex (safe navigation operator)

I've seen this somewhere, sometime before but I can't remember how it's used and I can't find anything about it online (Probably because I don't know what to search for). So please help me understand how to use this and also if you link a reference on the official documentation that would be great.

Boolean someVal = true;
String someString = someVal .? 'a value'
// results in someString = 'a value'

Boolean someVal = false;
String someString = someVal .? 'a value'
// results in someString = null

So it looks similar or maybe exactly like this code and I think it returns a null if the condition isn't true and returns 'a value' if the condition is true. Please let me know in the comments if I need to add clarification. Thanks!

Best Answer

The thing you're looking for is the safe navigation operator introduced in Winter '21 (API v50.0)

It's the reverse of what you thought it was, ?., and it helps us deal with nulls in a very compact fashion (as opposed to the previous null checks that we had to pepper all over the place).

Both of your examples would end up holding 'a value', because the variable you're testing has a defined value (false != null).

If that's not quite what you're looking for, then the next match would be the standard ternary operator, which has a syntax of <boolean expression> ? <value if true> : <value if false> such as Integer myInt = (2 + 2) > 3 ? 1 : -1;.