Direct answer, no it doesn't exist in java
Is there an equal for the c# null coalescing operator in java?
-
10-10-2022 - |
题
I searched a while for an equal for C# 'nullable' in java and found that one of the closest ways is to use the wrapper classes; now I want to know is there an equal for the c# null coalescing operator (??) in java? (of course except an ordinary 'if' statement)
解决方案 2
其他提示
No there isn't a null coalescing operator, however; there is a pattern that can be used in Java 8 that essentially does the same thing. Just wrap the result of an operation in an Optional and then call the orElse method
final String myString = Optional.ofNullable(resultSet.getString(1))
.orElse("default");
Guava provide an Objects.firstNonNull
.
不隶属于 StackOverflow