Domanda

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)

È stato utile?

Soluzione 2

Direct answer, no it doesn't exist in java

Altri suggerimenti

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top