Groovy Basics
Groovy Basics
Groovy Basics
Basics
Sang Shin
Michèle Garoche
http://www.javapassion.com
“Learn with Passion!”
1
Topics
• What is and Why Groovy?
• Refactoring Java code into Groovy code
• Differences from Java
• Inter-operating with Java
2
What is & Why Groovy?
Groovy
• Dynamic, objected oriented scripting language for
JVM
• Seamless integration with Java
> Designed with Java in mind from the beginning
(unlike other scripting languages)
> Easy to learn for Java programmers
• Borrowed language features from Ruby, Python,
Smalltalk
4
Why Groovy over Other Scripting Language?
• Dynamic language specifically designed for Java
platform
> Leverage the benefit of JVM
• Effortless transition from Java to Groovy
> Groovy code, when compiled, generated Java
bytecode
> Existing Java code work in Groovy environment “as
it is” basis
> Incremental change is possible in fact
recommended
5
Groovy IS Java (or BETTER Java)
• Provides syntactic sugar
> Easy and fun to code (like Ruby)
• Provides new language features over Java
> Dynamic typing
> Closure
> Meta-programming
• Provides easier development environment
> Scripting
> Combines compilation and execution into a single
step
> Shell interpreter
6
Why Groovy (or Scala)?
What is “State of Java”?
Java as a Programming Language
• Java programming language has been a huge
success but it is showing its age
• Java programming language has not evolved
significantly since JDK5 (2004)
• Java programming language is verbose
• Java programming language lacks modern
language features
> Closure, Meta-programming, DSL, Functional-
programming, Operator overloading, Regular
expression as a first class citizen, etc
• JDK7 is being delayed again
8
Java as a Platform (JVM)
• JVM is proven to be a great run-time platform
> Secure, highly performing, mature, etc
• We need better programming language over JVM
> Runs over JVM
> More productive, more fun, less verbose syntax
> With modern language features
> Seamless interoperability with Java programs
• Viable Choices
> Groovy
> Scala
> JRuby
> Clojure
9
Refactoring Java Code
into Groovy Code
Java Code - POJO
import java.util.List; public void setMessage(String Message) {
import java.util.ArrayList;
import java.util.Iterator; this.message = Message;
}
public class Blog {
private String name;
private String message; public static void main(String[] args) {
public Blog() {} List Blogs = new ArrayList();
Blogs.add(new Blog("1", "one"));
public Blog(String name, String Message) {
this.name = name; Blogs.add(new Blog("2", "two"));
this.message = Message;
} Blogs.add(new Blog("3","three"));
13
Differences from Java
Differences from Java
• Semicolons are optional.
> Use them if you like (though you must use them
to put several statements on one line).
• The return keyword is optional.
• You can use the this keyword inside static
methods (which refers to this class).
• Methods and classes are public by default.
• Attributes are private by default
• Inner classes are not supported at the moment.
In most cases you can use closures instead.
15
Differences from Java
• The throws clause in a method signature is not
checked by the Groovy compiler
> because there is no difference between checked
and unchecked exceptions.
• You will not get compile errors like you would in
Java for using undefined members or passing
arguments of the wrong type
• Basic packages are imported by default
16
Packages that are imported
• java.io.*
• java.lang.*
• java.math.BigDecimal
• java.math.BigInteger
• java.net.*
• java.util.*
• groovy.lang.*
• groovy.util.*
17
Inter-operating with Java
Groovy Ecosystem
Groovy Ecosystem
• Grails - Web application framework
• Griffon - MVC Desktop application framework
• Gant - Ant scripting language
• Spock - Testing framework
• Gaelyk - Toolkit for Google App Engine
• Gpars - Concurrent, Asynch system
• CodeNarc - Groovy code analyzer
• Many more
20
Thank you!
21