NOTIFICATION: These examples are provided for educational purposes. Using this code is under your own responsibility and risk. The code is given ‘as is’. I do not take responsibilities of how they are used.
Note: If you are not interested to know about the differences between Groovy and Java, skip to part 2.
Groovy is a dynamic language (which means many behaviors are executed at runtime instead of during compilation) based on the Java Virtual machine. This means that Groovy is tightly integrated with the Java language.
The biggest differences between Groove and Java are:
- The following packages and classes are imported by default (no need to use an explicit import to use them):
- These are optionals:
- Semicolons ‘;’ are optional
- The keyword ‘return’ is optional
- The keyword ‘this’ can be used inside static methods.
- Since in Groovy there is no difference between checked and unchecked exceptions, the clause ‘Throw’ in a method signature is not checked
- There is no compile errors for using:
- Undefined members
- Passing Arguments of the wrong type
- The == operator and the method is()
- The operator == will check for equality (checking if both object are equal) instead for identity (checking if both variables point the same object)
- The method is() allows for checking for identity.
- Example: foo.is(bar)
- This doesn’t work on null. For checking null, you can still using == operator
- New features added to Groovy not available in Java:
- Closures: piece of code which is defined and then executed at a later; also known as “code block” or method pointer.
- Lists and maps are native syntax which means that Groovy threads them as first class construct.
- Groovy provide native support for regular expressions
- Dynamic and static typing is supported
- Expressions can be embedded inside strings
- GPars (Groovy Parallel Systems) framework support for parallel processing. It supports:
- Map and Reduce
- Fork and Join
- GPath (Path Expression Language): Allows parts of nested structured data to be identified.
- GroovyMarkup: Native support for markup languages such as XML, HTML, W3C DOM, Ant Tasks, SAX, Swing, etc.
- Polymorphic interaction: Similar to STL standard iterators with these following differences:
- Declarations wrapped in traversable template which makes the collection accessible
- Constant iterator declaration is not scoped by the collection class. Instread the collection is passed in as a constructor argument.
- Switch statement:
- In java, we can only use the int or int-derived type classifier.
- In Groovy, we can any classifiers for a switch statement.
- Static and dynamic typing are supported.
- If you wish to use a dynamic types variable you can use the keyword ‘def’
- Writing beans with simpler syntax for both properties and adding even listeners.
- Simplified way to check null which eliminate nasty nested ifs
- All variables are reference to objects. Groove doesn’t use primitive variables.
- Even do you can write a primitive type as a short form, Groove will always translate it into the object.
Part 2 comming up soon…
© 2012, Alejandro G. Carlstein Ramos Mejia. All rights reserved.