Java Reference
In-Depth Information
Figure 6.2. The isPrime method has a bug, but the rest are fine.
The test detected that the Groovy implementation is returning true for all cases. The
Groovy implementation divides the given number by all the integers from 2 up to the
square root of the number minus 1, looking for any that come out even. That algorithm is
fine. The problem is that if a composite (non-prime) number is detected, the method is sup-
posed to return false.
Unfortunately, a return from inside a closure doesn't behave the way a regular Java de-
veloper expects. In fact, when you return from a closure it's like you're returning from a
method within another method. It only returns from the closure, not the method containing
it.
 
Search WWH ::




Custom Search