Archive for August 2013

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) .


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
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 :
    Evil Dead'''
println a
assert a in java.lang.String

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

Have a great day 🙂

Written by thangaveluaishwarya

August 25, 2013 at 9:57 PM

Groovy is NOT Java !

with one comment

Groovy is a Dynamic Scripting Language.

It follows the principle of Duck Typing, 

“If anything quacks,walks and swims like a duck then the object is considered to be a duck.”

So, if something possesses,
BEHAVIOURS  —>quacking, walking and swimming like a duck then
TYPE of that object –> Duck
Therefore based on the behaviour , the type is assigned to a variable.

In Groovy , we use the keyword def to assign any type to a variable.

Here’s an example to illustrate the above.

def a=10
assert a in java.lang.Integer

assert a in java.math.BigDecimal

assert a in java.lang.String

In the above we can notice few things,

1. Variable a has been assigned to multiple types . This is possible in groovy ,because ‘a’ has been defined as def, which means polymorphic substitution of values is possible.However,whatever value has been assigned finally, that will considered as the final type of a.

2. In the assert statements, java library has been used. Actually Groovy is the extension of Java. GDK(Groovy Development Kit) extends JDK, Java Libraries. Groovy code when compiled , gets converted to Java Bytecode and runs on the JVM.

Groovy follows Java Syntax. That’s why , it’s generally said that, Groovy would be easy for Java Developers.
However, I would like  to mark on the above statement that, though Groovy would be easy for a Java developer to understand, the style of Groovy coding is different from Java.
GROOVY IS NOT JAVA and has to be coded in the Groovy way.

If Groovy is the same as Java, then why should the Groovy Language ever be introduced ?

3. I haven’t included any class or method(especially main method) to execute my code because Groovy is a SCRIPTING Language.However, when we write an application, classes and methods are included for clarity.

4. Semi-colons are not used in any of the statements.

Yes, it is not a compulsary in Groovy , to use semi-colons to end the statement. Even, if you use it, it’s not a mistake.
If multiple assignments are being made in a single statement, then semi-colons can be used.

def a=10;  b= "I feel up";  c= a+b

In the above case, without semi-colon , you’ll not be able to distinguish and will get Runtime-Exception.

5. In a=’Aish’ The String has been quoted in Single quote.

Double quotes can also be used. Their difference, I will quote it in my next blog.

Groovy has got many advanced features like String Gstring usage, Operators functioning based on t type ,Easy Looping using Closures, minimising  Exceptional handling and etc.,

Groovier Updates coming up…

Written by thangaveluaishwarya

August 24, 2013 at 3:37 PM

Reflection – Dynamic Typing

with 4 comments

Today wanted to add some quick notes on my findings on Dynamic Languages.

To start with, lets look into Reflection.

REFLECTION” –  in common generic terms means a Mirror Image of something.

It carries the same meaning in Java. It means getting the mirror image of a class.
The microscopic details of a class can be explored during the runtime by specifying the class name dynamically. During this process the JVM interacts with the object of unknown type.

Let’s see an example,

public static void main(String[] args) {
	try {
		Class class1=Class.forName("aish.vaishno.musicstore.dao.MusicStoreDaoImpl");
		Package package1=class1.getPackage();
		System.out.println("Package Name with Version : "+package1);

		String canonicalName=class1.getCanonicalName();
		System.out.println("\nCanonical/Standard name of Field class: "+canonicalName);

		Method[] methods=class1.getMethods();
		System.out.println("\nMethods present in Field class:");
		for (int i = 0; i < methods.length; i++) {

                Class[] classes=class1.getInterfaces();
                System.out.println("Interface implemented: ");
                for (int i = 0; i < classes.length; i++) {
	} catch (ClassNotFoundException e) {

While specifying the className dynamically, the .class file of that particular class is opened and examined during the runtime .By Reflection, the package ,Method , Interface, modifier informations and etc.,can be retrieved.

But the demerits of this is ,

  • It slows down the performance
  • private fields and methods can be accessed.

Reflection concept is used in ,
Spring – while scanning through the packages.
ORM– when data from database is being mapped to the attributes in Entity Bean.
Likewise in Serialisation/Deserialisation, Remote Method Invocation and etc.,

Dynamic Language plays a major role in Rapid Application Development(RAD).

Groovy and Dynamic Language updates coming up.



Written by thangaveluaishwarya

August 9, 2013 at 9:48 PM