Friday, September 7, 2012

Say hello to Groovy – Part 2 (Operators & Control Structures)

In my last blog, I gave an introduction to Groovy. We looked at the typical hello world program in Java as well as Groovy to understand how some of the additional features provided by Groovy helps to cut down on a lot of boilerplate code that you would have otherwise written in Java. In this blog, I will concentrate on some of the operators and control structures which are unique to Groovy.

Groovy Truth

Like Java, Groovy supports standard conditional operators in a Boolean expression. In addition to this, any type can be evaluated as a Boolean expression in Groovy. This is possible because Groovy has rules to convert a non-boolean objects to a Boolean value. E.g. In case of string, non-empty strings evaluate to true


Following are some of the rules that you should know: 
1. Empty collection evaluates to false. 
2. Non-zero numbers evaluates to true. 
3. A non-null object reference evaluates to true. 
4. Non-empty Map evaluates to true. 
5. Iterators/enumerators evaluates to false if it has no further elements. 

Operator Overloading

In Java we use the == operator to see if the variables are referring to the same object instance. Groovy overrides == operator on types such as String, Integer, List etc., to check for content equality rather than object equality. To test if two variables are referring to the same object instance in Groovy we use the is () method.





Elvis Operator (?:)


The "Elvis operator" is similar to Java's ternary operator. It can be used for returning a 'sensible default' value if an expression resolves to false or null. E.g. In below snippet, if username is null or empty, “Anonymous” would get printed. 


Safe Navigation Operator (?.)


In Java, it’s a common practice to check for null, before invoking a method on the object to avoid NullPointerException. In Groovy, this is achieved in lesser lines of code using the Safe Navigation operator. The safe navigation operator will simply return null instead of throwing an exception, e.g: In the below code snippet, null would get printed and no NPE will be thrown.



Looping

In addition to all looping constructs available in Java, Groovy provides some extra ways to loop and execute a piece of code. Groovy extends the Integer class with the step (), upto () and times () methods. If we have a List in Groovy we can loop through the items on the list with the each () and eachWithIndex () methods. We will revisit this with examples when I cover closure and collections.


Exception Handling

Groovy doesn’t force you to handle exceptions within the method signature not even for checking exceptions. You are free to choose how and when you handle the exception. The exception is passed on to the calling code until it is handled. E.g. the following is a valid code in Groovy however if you were to write the same code in Java, the compiler would have forced you to handle java.net.MalformedURLException.


No comments:

Post a Comment