Thursday, December 26, 2013

Groovy Language Features

Will look at some of language features in Groovy that makes this language so dynamically groovy and easy to learn and use.

1. Less code more results.

Line terminating semicolon not required, unless more than one statements are being written on the same line.

Groovy imports the following packages by default:- java.lang.*, java.util.*, java.io.* and java.net.* so no explicit import statements are required. The following classes java.math.BigDecimal, java.math.BigInteger are also imported by default in addition to the groovy package groovy.lang.* and groovy.util.*

Classes and methods are public by default.

Groovy understand print and println, so no need to to have System.out.println. Return statement is always optional.

Java Version:










Groovy Version:










2. Groovy Data Types

Objects Objects Objects, everything is Groovy are objects even functions which are first class citizens and can be passed around as blocks or closures. Unlike Java which distinguishes between primitive types (boolean, byte, char, double, float, int, short) and references (Object and String), Groovy does not have any notion of primitive types in fact it uses the wrapper classes already available in the Java language :-
 java.lang.Byte
 java.lang.Short
 java.lang.Integer
 java.lang.Long
 java.lang.Float
 java.lang.Double
 java.lang.Character
 java.lang.Boolean
Groovy datatypes can be explicitly defined using types as in java








Groovy can implicitly determine the types dynamically when using the def key word.







3. Better Loops

Groovy supports the traditional for loop loop as in java :





In addition Groovy offers groovier ways of looping...
Range Loop
Range loop take the form 0..2 i.e start from 0 until 2.





Upto Loop
Upto loop takes an integer as an argument and expect a closure for the steps to execute.






Times Loop
Unlike the upto loop which takes an lower and upper bound, the times loop will execute for a fixed number of times.





Step Loop
As the name suggests the Step Loop allows step through an execution sequence by specifying lower and upper bounds and subsequent steps.

$it is a special variable which represents the current index in the loop being executed.

















4. Safe Navigation Operator (?.) and Elvis Operator (?:)

The safe navigator operator allows to eliminate redundant null checks. In most cases when one references an object we may need to verify if the object is null before invoking any actions on it. The safe navigation operator is a convenient way of letting groovy perform this check.





In the above code, the safe navigation operator will make sure that the dep object is valid and not null before returning the department code.

The Elvis operator is similar to the Java ternary operator and is used to evaluate statements with default values.






5. Equals and ==

In Groovy the is() method is equivalent to Java's identity method i.e. the java equals() method, where we check that the two objects are the same. 

The == is a smart equality check that can be used to check equality of values, Groovy will automatically take care of any null checks.










6. Groovy Beans

Groovy beans truly bring out the meaning of JavaBeans by defining what they truly are objects with properties.














There is less clutter when defining Groovy Beans. Properties are defined using def keyword or using explicit type definition as String, Integer etc.. Groovy will create getters and setter methods behind the scene.






When we call the member variables, we are executing the corresponding getter methods and not directly referring to the member variables.  If properties need to be made read only, the properties need to be declared with the final keyword in which case Groovy will only provide getter methods.










Name arguments can be used to construct Groovy Beans as well,











Until next time, Thank you
Malcolm

No comments:

Post a Comment