MyArchiBook

Quick Note 1: String and GString

with 3 comments

There are two different string classes used in Groovy and are java.lang.String and groovy.lang.GString.
Here String can be expressed using a single quote (‘abcd’) or a double quote (“abcd”).

Below is an example,

 def a='High-five'
assert a in java.lang.String

def b="High-five"
assert b in java.lang.String

def c=5; def d="High-$c"
assert d in groovy.lang.GString

From the above example, we can notice that variable a has its value declared in single quote and that of b in double quote. However both of them belong to the same class java.lang.String.

Just a variable’s value with double quote doesn’t make it a GString but, if a variable with double quote has another variable called inside it, by using $ symbol , then it is considered to be of GString type (groovy.lang.GString) .

BE CAREFUL: GString MISBEHAVES at times :


def a=1
def dummyMap=['item-1':'Cookies']

println dummMap.getClass()             //O/P: class java.util.LinkedHashMap
println dummyMap."item-$a"            // O/P: Cookies
println dummyMap.("item-$a")          // O/P: Cookies
println dummyMap["item-$a"]           // O/P: Cookies
println dummyMap.get("item-$a")       // O/P: null

println dummyMap.get("item-1")        //O/P: Cookies

There are different ways of retrieving values from a Map in Groovy. Few are listed above.

Though in the first place it appears that GString can be used to retrieve values by using $ symbol before the variable name , it also misbehaves at times.

An example is shown above .Misbehaviour in dummyMap.get(“item-$a”) . It provides a null value.

It’s now important for us to understand that ,GStrings are not like Java String.
Java Strings are immutable but GStrings can be mutated.

Check it out below.

Mutation in GString:

def number=5
def dummyString="High-$number"
println dummyString                                        //O/P: High-5
dummyString.values[0]=6
println dummyString                                        // O/P: High-6

Since GString is mutable , if used in key , then key is also prone to mutation.Therefore, it’s not a good practice to use a GString in key.

However, we can rectify this by converting GString to String. This is done by using toString() method.Once GString is converted to String, there wouldn’t be any problem because String is immutable.

Therefore, dummyMap.get(“item-$a”.toString()) will give us the proper output.

Otherwise,  dummyMap.getAt(“item-$a”) can be used. It is a Groovy method , so groovy implicitly takes care of the String conversion.

Whereas, dummyMap.get(“item-$a”) is a Java Method.Here we’ll have to explicitly specify toString() for conversion.

Apart from the above, multiple lines assigning can be done by using,

 Triple Single Quote ”’ and Triple Double quotes “””. 

It considers new lines (\n), white spaces, tabs (\t ) and etc.,

Triple Single Quote – reads Multiple Lines but is not supported by GString

Triple Double Quote – reads Multiple lines and is supported by GString.

a='''Horror Movies :
    Conjuring
    Evil Dead'''
println a
assert a in java.lang.String

def movieList=['Conjuring','Evil Dead','Grudge']
b="""Horror Movies :
    $movieList"""
println b
assert b in groovy.lang.GString

Have a great day 🙂

Advertisements

Written by thangaveluaishwarya

August 25, 2013 at 9:57 PM

3 Responses

Subscribe to comments with RSS.

  1. nice

    Ravi

    August 29, 2013 at 4:15 PM

  2. All the topics depth explanation. great

    santhosh

    August 29, 2013 at 4:52 PM

  3. Pls write more on such interesting topics. The above points described are very useful also we miss out to see the differences.

    Devi

    September 2, 2013 at 2:04 PM


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: