groovy basics

21
1 Groovy Language Groovy Language Basics Basics Sang Shin Sang Shin Michèle Garoche Michèle Garoche http://www.javapassion.com http://www.javapassion.com Learn with Passion!” Learn with Passion!” 1

Upload: arogya-kiran-babu

Post on 06-Apr-2015

103 views

Category:

Documents


1 download

DESCRIPTION

scripting language for java

TRANSCRIPT

Page 1: Groovy Basics

1

Groovy LanguageGroovy LanguageBasicsBasics

Sang ShinSang ShinMichèle GarocheMichèle Garoche

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

1

Page 2: Groovy Basics

2

Topics• What is and Why Groovy?• Refactoring Java code into Groovy code• Differences from Java• Inter-operating with Java

Page 3: Groovy Basics

What is & Why Groovy?What is & Why Groovy?

Page 4: Groovy Basics

4

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

Page 5: Groovy Basics

5

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

Page 6: Groovy Basics

6

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

Page 7: Groovy Basics

Why Groovy (or Scala)?Why Groovy (or Scala)?What is “State of Java”?What is “State of Java”?

Page 8: Groovy Basics

8

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

Page 9: Groovy Basics

9

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

Page 10: Groovy Basics

Refactoring Java CodeRefactoring Java Codeinto Groovy Codeinto Groovy Code

Page 11: Groovy Basics

11

Java Code - POJOimport java.util.List;import java.util.ArrayList;import java.util.Iterator;

public class Blog { private String name; private String message;

public Blog() {}

public Blog(String name, String Message) { this.name = name; this.message = Message; }

public String getName() { return name; }

public void setName(String name) { this.name = name; }

public String getMessage() { return message; }

public void setMessage(String Message) { this.message = Message; }

public static void main(String[] args) { List Blogs = new ArrayList(); Blogs.add(new Blog("1", "one")); Blogs.add(new Blog("2", "two")); Blogs.add(new Blog("3","three"));

for(Iterator iter = Blogs.iterator();iter.hasNext();) { Blog Blog = (Blog)iter.next(); System.out.println(Blog.getName() + " " + Blog.getMessage()); } }}

Page 12: Groovy Basics

12

Groovy Code - POGOclass Blog { String name String message}

def blogs = [ new Blog(name:"1", message:"one"), new Blog(name:"2", message:"two"), new Blog(name:"3", message:"three")]

blogs.each { println "${it.name} ${it.message}"}

• No more import statement

• No more getter/setter methods for properties

• No more constructor method

• No more semicolor ;• No more parenthesis in

a method call• No type specification

Page 13: Groovy Basics

13

Groovy Code - POGOclass Blog { String name String message}

def blogs = [ new Blog(name:"1", message:"one"), new Blog(name:"2", message:"two"), new Blog(name:"3", message:"three")]

blogs.each { println "${it.name} ${it.message}"}

• No more ArrayList class> Use [..] notation

• No more “for” loop> Use closure

• No more main() method> main() method gets

added by Groovy• No more

System.out.println()> Use println

Page 14: Groovy Basics

Differences from JavaDifferences from Java

Page 15: Groovy Basics

15

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.

Page 16: Groovy Basics

16

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

Page 17: Groovy Basics

17

Packages that are imported• java.io.*• java.lang.*• java.math.BigDecimal• java.math.BigInteger• java.net.*• java.util.*• groovy.lang.*• groovy.util.*

Page 18: Groovy Basics

Inter-operating with JavaInter-operating with Java

Page 19: Groovy Basics

Groovy EcosystemGroovy Ecosystem

Page 20: Groovy Basics

20

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

Page 21: Groovy Basics

21

Thank you!Thank you!

Check JavaPassion.com Codecamps!Check JavaPassion.com Codecamps!http://www.javapassion.com/codecampshttp://www.javapassion.com/codecamps

““Learn with Passion!”Learn with Passion!”

21