Massimo Caliman
by Massimo Caliman
~1 min read

Modified

Languages

  • English

Categories

  • Java

Tags

  • Java
  • Programming Languages

“An algorithm must be seen to be believed.” – Donald Knuth

In C# the ?? operator is called the null-coalescing operator. Here’s an example of use:

string result = leftHand ?? rightHand ;

It returns the left-hand operand if it is not null, otherwise it returns the right-hand operand. A possible alternative in Java of C# ?? use ? , the ternary operator, like this:

String result = leftHand != null ? leftHand : rightHand; 

Or, by using a method like this:

String nullCoalescingOperator(String leftHand, String rightHand) {
    return leftHand != null ? leftHand : rightHand;
}