Java Reference
In-Depth Information
Figure 1.2. Arrays and strings from a duck-typing point of view. Each is a collection with an append method. If
that's all we care about, they're the same.
We don't care that a string is not an array as long as it has the append method we need.
This example also showsanother feature ofGroovythat was left out ofJava: operator over-
loading. In Groovy all operators are represented by methods that can be overridden. For
example, the + operator uses a plus() method and * uses multiply() . In the previ-
ous figure the << operator represents the leftShift() method, which is implemented
as append for both arrays and strings.
Groovy Feature
Groovyfeatures like optional typing and operator overloading give developers greater flex-
ibility in far less code.
Regarding optional typing, Groovy gives you the best of both worlds. If you know the type
of a variable, feel free to specify it. If you don't know or you don't care, feel free to use the
def keyword.
 
Search WWH ::




Custom Search