Groovy中变量后面的问号(?)的目的是什么 [英] What is the purpose of question mark(?) after a variable in Groovy

查看:36
本文介绍了Groovy中变量后面的问号(?)的目的是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 grails 的新手,我在许多示例中发现变量可能以问号 (?)像这样

I am new to grails I found in many examples that a variable may end with question mark (?) like this

boolean equals(other) {
    if(other?.is(this))
    return true
}

上面的代码包含 If 条件,其中另一个以 ?所以我想知道那个表示的含义

above code contains If condition in that other is ending with a ? so I want to know the meaning of that representation

推荐答案

?. 是一个空安全操作符,用于避免意外的 NPE.

?. is a null safe operator which is used to avoid unexpected NPE.

if ( a?.b ) { .. }

if ( a != null && a.b ) { .. }

但在这种情况下 is() 已经是空安全的,所以你不需要它

But in this case is() is already null safe, so you would not need it

other.is( this )

应该不错.

这篇关于Groovy中变量后面的问号(?)的目的是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆