top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is an elvis operator?

+1 vote
367 views

I have heard so much about this operator but didn't know the best use cases.

posted Sep 9, 2017 by Shivam Kumar Pandey

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote
 
Best answer

the Elvis operator ?: is a binary operator that returns its first operand if that operand is true, and otherwise evaluates and returns its second operand.

In a language that supports the Elvis operator, something like this:

x = f() ?: g()

will set x equal to the result of f() if that result is a true value, and to the result of g() otherwise.

It is equivalent to this example, using the Ternary Operator:

x = f() ? f() : g()

except that it does not evaluate the f() twice if it is true.

Credit:Wiki

answer Sep 9, 2017 by Salil Agrawal
Similar Questions
+1 vote

Example: consider this string
" Parse this date string 10 juil 2014 @@@parse date " OR "10 juil 2014" (This is easy with SimpleDateFormat using Locale.French) but I want the solution to extract date first from the string and then convert to Date using SimpleDateFormat.

+1 vote

like prepared Statement I want to set property to query at runtime so how to do that in grails frameworks?

...