MyArchiBook

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

a=0.10
assert a in java.math.BigDecimal

a='Aish'
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…

Advertisements

Written by thangaveluaishwarya

August 24, 2013 at 3:37 PM

One Response

Subscribe to comments with RSS.

  1. was of thinking that you will use groovy soon. what made you use groovy?

    Steve

    August 24, 2013 at 4: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: