1

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!

2
  • 4
    The syntax you're using suggests it's the new safe navigation operator
    – Derek F
    Commented Feb 23, 2021 at 20:09
  • @DerekF thanks much! That's exactly what I was looking for. Would you mind posting your answer so I can select it?
    – Bahman.A
    Commented Feb 23, 2021 at 20:11

1 Answer 1

3

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;.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .