Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an equal for the c# null coalescing operator in java?

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)

like image 864
mohsen kamrani Avatar asked Feb 12 '14 06:02

mohsen kamrani


People also ask

What is the == operator in C?

== operator The '==' operator checks whether the two given operands are equal or not. If so, it returns true. Otherwise it returns false.

Does C use ==?

== is an Equal To Operator in C and C++ only, It is Binary Operator which operates on two operands. == compares value of left and side expressions, return 1 if they are equal other will it will return 0.

What is -= in C?

-= Subtract AND assignment operator. It subtracts the right operand from the left operand and assigns the result to the left operand. C -= A is equivalent to C = C - A.

What does += mean in C?

+= Add AND assignment operator. It adds the right operand to the left operand and assign the result to the left operand. C += A is equivalent to C = C + A.


1 Answers

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");
like image 106
Jason Thompson Avatar answered Oct 08 '22 22:10

Jason Thompson