我知道这对我来说可能非常不合时宜,但我需要能够在数组中存储空字符串。根据我的发现,它看起来像这样:string = ''最终与 this 相同string = null。
这是真的还是我错过了什么?
如果这是真的,我怎样才能初始化一个字符串并使其在groovy中为空?
Groovy 不认为''等于null。
'' == null
===> false
Run Code Online (Sandbox Code Playgroud)
也许您是在布尔表达式的上下文中读到的,其中两者是等效的(并且if(null)和 的if('')计算结果都为 false)。
您可以按照通常的方式声明一个字符串:
String str = ''
def str = ''
Run Code Online (Sandbox Code Playgroud)