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

Direct answer, no it doesn't exist in java

其他提示

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top