Vous êtes sur la page 1sur 21

Groovy Language

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"));

public String getName() {


return name; for(Iterator iter = Blogs.iterator();iter.hasNext();) {
} Blog Blog = (Blog)iter.next();
public void setName(String name) { System.out.println(Blog.getName() + " " +
this.name = name; Blog.getMessage());
}
}
public String getMessage() { }
return message;
}
}
11
Groovy Code - POGO
class Blog {
String name
• No more import
String message statement
}
• No more getter/setter
def blogs = [
new Blog(name:"1", message:"one"),
methods for properties
new Blog(name:"2", message:"two"),
new Blog(name:"3", message:"three")
• No more constructor
] method
blogs.each { • No more semicolor ;
println "${it.name} ${it.message}"
} • No more parenthesis in
a method call
• No type specification
12
Groovy Code - POGO
class Blog { • No more ArrayList class
String name
String message > Use [..] notation
}
• No more “for” loop
def blogs = [ > Use closure
new Blog(name:"1", message:"one"),
new Blog(name:"2", message:"two"), • No more main() method
new Blog(name:"3", message:"three") > main() method gets
] added by Groovy
blogs.each { • No more
println "${it.name} ${it.message}" System.out.println()
}
> Use println

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!

Check JavaPassion.com Codecamps!


http://www.javapassion.com/codecamps
“Learn with Passion!”

21

Vous aimerez peut-être aussi