Subscribe to The Way I See It        RSS Feed
***** 1 Votes

Totally Groovy

Icon Leave Comment
As promised, I'm going to start a blog on my new favorite language, Groovy. New is a rather relative term since it was initially released in 2004 by James Strachan. It hasn't received a lot of hype, but I think it's the coolest thing around. Groovy is an object-oriented functional language for the Java platform--It's code is compiled with the JVM dynamically. It works with most previously existing Java libraries, making most Java code completely valid. The language was developed with Java people in mind. Starting off, there was almost no difference between my Java code and Groovy code, but after a few days, I started unlocking some of the goodies Groovy had to offer me. You might notice some similarities with other functional programming languages.

Imports (and syntactic sugars)

First of all, Groovy makes life so easy for Java developers by treating us like we know what we are doing. Right off the bat, it imports a few very standard Java libraries that we all use regularly:

  • java.lang.*
  • java.math.BigDecimal
  • java.math.BigInteger
  • java.util.*
  • groovy.lang.*
  • groovy.util.*

It's so nice because on a program to program basis, my imports are completely empty, making it look crisp and clean. Another very cool deal is the lack of semi-colons. Groovy doesn't care either way! If you're a die-hard semicolon fan, feel free to plop them in, but they aren't needed. It's nice to know I won't be yelled at for leaving one or two out after a long day of pounding away at the key board:wink:

Dynamic Typing and Variables
One of the most basic things Groovy offers is dynamic typing using def. Dynamic typing happens at runtime as opposed to static typing that occurs during compile time. Consider def to be akin to Object in java and the light bulb will come on. There are of course deeper theoretical concepts behind this but that's for another time. Here is an example of the flexibility we have:

String normal = "Good in java"
normal = 1 //very bad--statically typed

def groovy = "groovy"
groovy = 1 // in a variable of dynamic type (Object-esce)

Since Groovy is a scripting language, it's important to note that it handles variables like a script does as well. When a variable is defined, it is always local and methods are not included in the scope. Here is a small snippet from Groovy's informational site:

String attribute = "bar"
void aMethod(){
  assert attribute == "bar" // Not allowed !

attribute is a local variable and therefor isn't visible in aMethod. This might be one of the most difficult things for a person in a Java background to remember: defined variables are local.

Groovy operates heavily with closures. Simply put, they are method blocks stored in variables

def square = {number -> number * number}
square(2) // returns 4

I wish I could keep writing but I have some errends to run, but until then, check out Groovy for homework :wink:
Next time: Fun with Maps, Lists, the 'it' variable, and Collection operation

0 Comments On This Entry


Trackbacks for this entry [ Trackback URL ]

There are no Trackbacks for this entry

December 2021

   123 4

Recent Entries

Recent Comments

Search My Blog

2 user(s) viewing

2 Guests
0 member(s)
0 anonymous member(s)