Safe navigation operator in Groovy

 The Safe navigation operator(?.) is used for eliminating "Null" check in Groovy. See the below example.
 In java the null value check is like this.

   String str = null;
   if(str !=null) {

Same thing in Groovy.

class SafeNavigationOperatorDemo {
    public static void main(String[] args) {
        def str = null;
        println str?.reverse()
        str = "Cool stufs...."
        println str?.reverse()

This will print the message like this.
....sfuts looC


Popular Posts