Java Reference
In-Depth Information
The closure tells the sort method to use the result of the size() method on each ele-
ment to do the sorting, with the minus sign implying that here I'm asking for descending
order.
Groovy Feature
Groovy's additions to the JDK simplify its use, and Groovy closures eliminate artificial
wrappers like anonymous inner classes.
There were two major productivity improvements in this section. First, there are all the
methods Groovy added to the Java libraries, known as the Groovy JDK. I'll return to those
methods frequently. Second, I take advantage of Groovy's ability to treat methods as ob-
jects themselves, called closures. I'll have a lot to say about closures in the upcoming
chapters, but the last example illustrated one advantage of them: you almost never need an-
onymous inner classes.
Incidentally, in the closure I used an additional Groovy feature to protect myself. The ques-
tion mark after the word it is the safe de-reference operator. If the reference is null it in-
vokes the size method here. If not it returns null and avoids the Null-PointerEx-
ception . That tiny bit of syntax wins over more Java developers to Groovy than I ever
would have believed. [ 4 ]
4 Sometimes they get tears in their eyes. “Really?” they say. “I don't have to put in all those null checks?” It's touch-
ing how happy they are.
1.1.3. Java is overly verbose
The following listing shows a simple POJO. In this case I have a class called Task , per-
haps part of a project management system. It has attributes to represent the name, priority,
and start and end dates of the task.
 
Search WWH ::




Custom Search