Java Reference
In-Depth Information
The closure supplied to the eachWithIndex method takes two dummy arguments. The
first is the value from the collection, and the second is the index.
I should mention that although all these loops work correctly, there can be differences in
how much time each of them takes. If you're dealing with a collection of a few dozen ele-
ments or less, the differences will probably not be noticeable. If the number of iterations
is going to be in the tens of thousands or more, you probably should profile the resulting
code.
B.6.2. Conditionals
Java has two types of conditional statements: the if statement and its related constructs,
like if-else and switch statements. Both are supported by Groovy. The if statement
works pretty much the same way it does in Java. The switch statement, however, has
been taken from Java's crippled form and restored to its former glory.
Groovy's version of the if statement is similar to Java's, with the difference being the so-
called Groovy Truth. In Java, the argument to an if statement must be a Boolean expres-
sion, or the statement won't compile. In Groovy, lots of things evaluate to true other than
Boolean expressions.
For example, nonzero numbers are true:
if (1) {
assert true
} else {
assert false
}
The result is true . This expression wouldn't work in Java. There you would have to com-
pare the argument to another value, resulting in a Boolean expression.
Return to C?
The Groovy Truth is a case where Java restricted something C supported (non-Boolean ex-
pressions in decision statements), but Groovy brought it back. That can certainly lead to
bugs that Java would avoid.
Search WWH ::




Custom Search