MyArchiBook

Groovy-2.X tidbits

leave a comment »

Did you know …?

1.We can know the last element of an array by using -1 as the index
Eg:
def a=[1,2,3,4,5,6,7]
assert a[-1]==7

2.Groovy is much like an English speaking language
Eg :   for loop
Earlier in Java,

for(int i=1;i<=10;i++){
 System.out.println("Java");
}

*NOTE: Now that Java 8 has introduced Instream for loop with range.

In Groovy,

10.times{
 println 'Groovy'
}

3.Default access specifiers

  • for Class -> public
  • for Method -> public
  • for variables -> private

4.Groovy generates 2 constructors implicitly
(1) a public constructor
(2) a public constructor where its class variables can be passed as parameters.

5.Values in the constructor can be assgined via Implicit coersion and Explicit coersion too
Eg:

class Season{
def type
def month
}

//Normal way
def s1=new Season(type:"winter",month:"December")

//Implicit coersion - pre-defining the type of variable.
Season s3=[type:"autumn",month:"September"]

//Explicit coersion using 'as'
def s2=[type:"summer",month:"May"] as Season

6.Underscores can be used in number literals for better understanding
Eg:

long creditCardNumber=1234_2345_3456_4567L

Advancements in languages is happening day by day.
Groovy beta version has come up with advanced features like Delegating scripts, TypeChecking Extensions in Script, project coin(present in Java7) and etc.,However Java 8 is also coming up with much advanced features.

We should bear in mind that language should be only the means to make things simpler.
It’s wise to choose the right one based on our needs.

Advertisements

Written by thangaveluaishwarya

February 9, 2014 at 2:46 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: