Sunday, August 12, 2012

Groovy Day 1 : Truth (2/2)

The second part of the post on Groovy Truth is about collections and custom conversion to boolean. First the collections, in Groovy, a collection can be tested for nullity and emptiness at the same time.
List list1 = [2,3,5] // way that groovy declare List, in this case, an ArrayList is used
List list2 = [] // empty list
List list3 = null

assert list1 // test is the object is null, if it's not the case, test emptiness on collections
assert !list2 // the list is non-null but empty
assert !list3 // the list is null

Groovy Day 1 : Truth (1/2)

This post is the first of the serie on Groovy discovery. To start easily the learning of Groovy, we need to understand the differences with Java. Groovy is a language that is compiled in bytecode like Java. The bytecode is then read by the same JVM. In other words, Groovy can use Java library (all the .jar that you can find in the internet, quite amazing) and Java can use Groovy library too ! The magic of bytecode in some sense. We could also imagine an interaction with Scala or JRuby etc. all the JVM language are intercompatible. This was for introduction, let's discover the first real part. The Groovy Truth is something very weird for an old Java developper. In Java we have some really particular cases to learn and understand to avoid common mistakes.