You are on page 1of 15

Module 1

Groovy Fundamentals
Module Overview

Differences between Groovy and Java


Declaring classes
Using assertions
Groovy Strings
Closures
Lists and Maps
Expandos
Ranges
Lists, Maps, and Sets
Builders
Metaprogramming
Java An Overview

Object Oriented
Platform Independent
Simple
Secure
Architecture-neutral
Portable
Robust
Multithreaded
Interpreted
High Performance JIT
Distributed
Dynamic
Intro. To Groovy

OOP
Through Apache Licence
Optionally compiled
Runs in JVM
Compiles down to Java byte-code
Simplified syntax
Support both Static and dynamically typed
Support for operator overloading.
operator overloading
Native syntax for lists and associative arrays
Native support for regular expressions
Native support for various markup languages
You can use existing Java libraries
Groovy extends the java.lang.Object
Differences between Groovy and Java

Default imports
Multi-methods
Array initializers
Package scope visibility
ARM blocks - Automatic Resource Management
Inner classes
Static inner classes
Anonymous Inner Classes
Creating Instances of Non-Static Inner Classes
Lambdas
GStrings
String and Character literals
Primitives and wrappers
Behaviour of ==
Conversions
Extra keywords
Default imports

All these packages and classes are imported by default


java.io.*
java.lang.*
java.math.BigDecimal
java.math.BigInteger
java.net.*
java.util.*
groovy.lang.*
groovy.util.*
Multi-methods

methods which will be invoked are chosen at


runtime.
Same in Java and Groovy, but it will behave differently:
int method(String arg) { return 1; }
int method(Object arg) { return 2; }
Object o = "Object"; int result = method(o);
Array initializers

{ } block is reserved for closures.


Unsupported
int[] array = { 1, 2, 3}
Supported
int[] array = [1,2,3]

Package scope visibility

omitting a modifier on a field doesnt result in a


package-private field like in Java:

Instead, private field, an associated getter and an


associated setter
to create a package-private field by annotating it
with @PackageScope
Closures

Following

Can be written as
Inner classes

Static inner classes


Anonymous Inner Classes
Creating Instances of Non-Static Inner Classes
Static inner classes
Anonymous Inner Classes
Creating Instances of Non-Static Inner Classes

UnSupported Supported
Lambdas

Groovy Java
Java 8 supports lambdas Has Closures
Runnable run = () -> Runnable run = { println
System.out.println("Run 'run' } list.each { println
"); it } // or
list.forEach(System.out: list.each(this.&println)
:println);

You might also like