DEV Community

loading...

Discussion on: Language Features: Best and Worst

Collapse
awwsmm profile image
Andrew (he/him) Author • Edited

Is this:

nullObject?.somefunction() ?? "this is nice sometimes"

...a ternary operator? Like

nullObject == null ? somefunction() : "this is nice sometimes"

...in Java?

Collapse
vberlier profile image
Valentin Berlier

Yeah but you can chain it so it makes things a lot more readable:

foo?.bar?.getResult()?.toString()
Enter fullscreen mode Exit fullscreen mode
Thread Thread
awwsmm profile image
Andrew (he/him) Author

Nice! That's a cool little operator.

Thread Thread
greenroommate profile image
Haris Secic

a bit late but yeah.. Valentin answerd already but ?. checks for null and prevents null pointer exception and returns null for value but also ?? is null coalescing operator which in case something returns null takes right side value as in java ternary operator but this one's shorter so nullvaluestring ?? "somedefaultvalue" looks a bit prettier :D

Forem Open with the Forem app