Reputation: 117
I know this may be very un-groovy of me, but I need to be able to store an empty string in an array. Based on what I have found it seems like this: string = ''
ends up being the same as this string = null
.
Is this true or am I missing something?
If it is true, how can I initialize a string and make it empty in groovy?
Upvotes: 4
Views: 22616
Reputation: 3932
You can add an empty string to an array, are you trying to perform some logic based on the values of the array that's making use of the groovy truth maybe?
def myArr = new String[3]
myArr[0] = 'hello'
myArr[1] = ''
myArr[2] = null
myArr.each{ println it }
// prints
hello
null
// whereas the following...
myArr.each{ if (it) println it }
// prints
hello
// and nothing else
Upvotes: 2
Reputation: 45319
Groovy does not consider ''
as equal to null
.
'' == null
===> false
Perhaps you read that in the context of boolean expressions, where the two are equivalent (both if(null)
and if('')
evaluate to false).
You can declare a string the usual way:
String str = ''
def str = ''
Upvotes: 5