Java Reference
In-Depth Information
As mentioned earlier, the script lacks semicolons. Semicolons as statement separators are
optional in Groovy and can be omitted if there's no ambiguity. Again, you're free to add
them in without a problem.
Semicolons
In Groovy, semicolons work but are optional.
Next,Groovyusesthemethodcalled assert extensively.Theword assert canbewrit-
ten without parentheses, as done here, or you can surround an expression with them. The
resulting expression must evaluate to a Boolean, but that's a much looser requirement than
in Java. In Java, the only available Booleans are true and false . In Groovy, non-null
references are true, as are nonzero numbers, non-empty collections, non-empty strings, and
the Boolean value true .
That bears repeating and goes by the term The Groovy Truth .
The Groovy Truth
In Groovy, non-null references, non-empty collections, non-empty strings, nonzero num-
bers, and the Boolean value true are all true.
Finally,thedefaultdatatypeforfloating-pointvaluesinJavais double ,butinGroovyit's
java.math.BigDecimal . The double type in Java has approximately 17 decimal
places of precision, but if you want to get depressed about its accuracy, try this tiny sample:
println 2.0d - 1.1d
The d appended to the literals makes them doubles. You would expect the answer here
to be 0.9, but in fact it's 0.8999999999999999. That's not much of a difference, but I've
only done a single subtraction and I'm already off. That's not good. That's why any serious
numerical calculations in Java require java.math.BigDecimal , but that means you
can't use the standard operators ( + , - , * , / ) anymore and have to use method calls instead.
Search WWH ::




Custom Search