

== LEARN GROOVY


=== Syntax

Groovy's syntax can be made far more compact than Java. For example, a declaration in Standard Java 5+ such as:


[source,java]

----

 for (String it : new String[] {"Rod", "Carlos", "Chris"})
     if (it.length() <= 4)
         System.out.println(it);

----

can be expressed in Groovy as:


[source,java]

----

 ["Rod", "Carlos", "Chris"].findAll{it.size() <= 4}.each{println it}

----

What you see in the parentheses \{ \} is a magical *Closure * . What make Groovy so powerful compare to Java!
We will learn about it later.



==== Official syntax description

link:http://groovy.codehaus.org/For+those+new+to+both+Java+and+Groovy[http://groovy.codehaus.org/For+those+new+to+both+Java+and+Groovy]


link:http://groovy.codehaus.org/User+Guide[http://groovy.codehaus.org/User+Guide]


link:http://groovy.codehaus.org/Logical+Branching[http://groovy.codehaus.org/Logical+Branching]


link:http://groovy.codehaus.org/Looping[http://groovy.codehaus.org/Looping]


For your overview, I will introduce the most important syntax of the language:



==== Logical Branching


==== Looping


==== Operation


=== Highlight language features

Beside with elegant syntax, Groovy has features you always wish that Java have, below paragraph will call out few features that actually help a lot in Game programming:



==== Operation overload


==== Collections


==== Reflection


=== Closure

Read more about it here:


link:http://groovy.codehaus.org/Closures[http://groovy.codehaus.org/Closures]
link:http://groovy.codehaus.org/Closures+-+Informal+Guide[http://groovy.codehaus.org/Closures+-+Informal+Guide]
link:http://groovy.codehaus.org/Closures+-+Formal+Definition[http://groovy.codehaus.org/Closures+-+Formal+Definition]


What is a *Closure*?


A Groovy Closure is like a “code block or a method pointer. It is a piece of code that is defined and then executed at a later point. It has some special properties like implicit variables, support for currying and support for free variables (which we'll see later on). We'll ignore the nitty gritty details for now (see the formal definition if you want those) and look at some simple examples.
Simple Example


[source,java]

----

def clos = { println "hello!" }

println "Executing the Closure:"
clos()                          //prints "hello!"

----

Note that in the above example, “hello! is printed when the Closure is called, not when it is defined.



=== and Gotchas

link:http://groovy.codehaus.org/Differences+from+Java[http://groovy.codehaus.org/Differences+from+Java]



=== Meta-programming

Here come the Good! For those who remember the power of JavaScript’s prototype keyword, you can do them same with Groovy. For those who don’t even know what JavaScript is, Meta-programming is the way to expand the class with methods and attributes on the fly. 



=== Groovy Builder – SwingBuilder

Groovy use a lot power the of Builder pattern.


In the snippets below I will show you write a few builders to speed up your JME code. The full list of Builder come in Part3 and 4.



=== Language comperation


==== JavaScript comperation
