Groovy & Java 8
A very simple comparison
@ JaverosMX, Mexico City
30/Marzo/2015Domingo Suárez Torres (@domix)
• A little bit about Groovy
• Why is important to upgrade to Java 8
• Very small :’(
Groovy, just a little
• Optionally typed & dynamic language for the
• Static-typing and static compilation capabilities
• Scripting capabilities, Domain-Speciﬁc
Language authoring, runtime and compile-time
meta-programming and functional programming
Seamlessly and transparently
integrates and interoperates with
Java and any third-party libraries
Java 8 matters
• Java 7 EOL. April 2015
• Java 6 < go ﬁgure.
• “Brand new” techniques, patterns, paradigms. Built in the language.
• Very soon libraries and frameworks will adopt those improvements.
• Java 8 arrived late to the party. Better late than never.
Java 8 features
• New Syntax
• Stream API
• Date/Time API
• Java Mission Control
Code as data
• Interfaces with one method (functional interface)
• Single Abstract Method interfaces (SAM Interfaces)
• Inner classes are annoying due verbosity.
• Java 8 Lambda Expressions, Groovy Closures.
• JVM functions became almost ﬁrst-class
• Since 2003.
• Is an open, anonymous, block of code that can take
• Return a value and be assigned to a variable.
• A closure may reference variables declared in its
• Can also contain free variables which are deﬁned outside
of its surrounding scope. In opposition to the formal
deﬁnition of a closure.
• Represents a sequence of elements on which one or
more operations can be performed.
• Stream operations are either intermediate or terminal.
• While terminal operations return a result of a certain type,
intermediate operations return the stream itself so you
can chain multiple method calls in a row.
• Streams are created on a source (Collection)
• Stream operations can either be executed sequential or
• Groovy add lots of enhancements into the
• Groovy “injects” additional methods on the
standard Java libraries
• GDK adds syntactic sugar to Java APIs for
better and easier usage.
• NPE is awkward.
• A optional is a container object which may or
may not contain a non-null value.
• Additional methods that depend on the
presence or absence of a contained value are
provided, such as orElse() (return a default value
if value not present) and ifPresent() (execute a
block of code if the value is present).