Pergunta

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)

Foi útil?

Solução 2

Direct answer, no it doesn't exist in java

Outras dicas

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top