June 5, 2008

Numbers the Groovy way

So far I'm still at the beginning of my journey into the wonderful world of scripting with the Java based Groovy. I'm seeing a lot of neat things, but still I'm just scratching the surface. One thing that I see so far that is really useful is the way numbers are handled.

In Groovy, everything is an object. There are NO native primitive types such as Java (which is Object Oriented with a handful of primitive exceptions to the rule). So in early iterations of Java you had to manually box your primitives into objects to work with them in Collections and other Object based processes.

Groovy has that covered. In Groovy you can reference a number just like an object (because it is an object). Then if you need to make a List of int's it's boxed on the fly because there is no int but everything that would be a Java int is and Integer (auto-boxing to the extreme).

// using the Groovy built-in function .times you can
// simply say you want to do something a number of times
// without having to setup a full fledged for loop
10.times
{
println "my line"
}

That's just one sample, but I hope it show's you how simple it is to work with numbers in this groovy scripting language.

Until next time
Les

No comments: