Elvis operator : Java Glossary


Elvis operator

Groovy has the ?: operator called the Elvis operator because it looks a bit like Elvis Presley’s hair. Java does not support it. It is helpful in dealing with values that might be null. It is a shorcut for a common piece of code to deal with null values. If the Elvis operator were introduced to Java:

// Instead of writing:
val = something != null ? something : defaultValue;

// you would write:  
val = something ?: defaultValue;

This page is posted
on the web at:


Optional Replicator mirror
of mindprod.com
on local hard disk J:

Canadian Mind Products
Please the feedback from other visitors, or your own feedback about the site.
Contact Roedy. Please feel free to link to this page without explicit permission.

Your face IP:[]
You are visitor number