3 ms·
I'm surprised by how verbose some of these new APIs are, like: Objects.requireNonNullElse(str, "default") or even worse, if you need the right-hand side t
by sltkr 2y ago
I'm surprised by how verbose some of these new APIs are, like:
Objects.requireNonNullElse(str, "default")
or even worse, if you need the right-hand side to be evaluated lazily:
Objects.requireNonNullElseGet(str, () -> "default")
while Javascript's null-coalescing operator allows writing this so much simpler:
str ?? "default"
str ?? getDefault()
(And not to mention the fact that the null-coalescing operator can also be used for conditional assignment, for which Java has no option at all!)
- ziml77 2y agoI thought this post might be a joke because it claimed that it was saving verbosity here! ?? is also the null coalescing operator in C#. Not only is it nice and short, but since it's an infix operator it chains in a much cleaner way than nesting static method calls.