Groovy & Java 8A very simple comparison @ JaverosMX, Mexico City
30/Marzo/2015Domingo Suárez Torres (@domix)
Agenda
• Background
• A little bit about Groovy
• Why is important to upgrade to Java 8
• Comparison
• Very small :’(
Groovy, just a little
• Optionally typed & dynamic language for the JVM
• Static-typing and static compilation capabilities
• Scripting capabilities, Domain-Specific Language authoring, runtime and compile-time meta-programming and functional programming
Java 8 matters• Java 7 EOL. April 2015
• Java 6 < go figure.
• “Brand new” techniques, patterns, paradigms. Built in the language.
• Very soon libraries and frameworks will adopt those improvements.
• Enhancements
• Nashorn
• Invoke-dynamic
• Java 8 arrived late to the party. Better late than never.
Java 8 features• New Syntax
• Stream API
• Profiles
• JavaFX
• Date/Time API
• Nashorn
• Pack200
• JDBC
• Concurrency
• JAXP
• Hotspot
• Java Mission Control
Java 8 syntax• Lambda expressions
• Method references
• static & default methods in interfaces
• Repeating annotations
• Annotation on types
• Improved type inference
• Method parameter reflection
Feature similarities
• Java 8
• Lambda expressions
• Default methods
• Optional types
• Stream API
• Method references
• Groovy
• Closures
• Traits
• Truth & null handling
• GDK Collections
• Method closures
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 first-class citizens.
Groovy Closures• Since 2003.
• Is an open, anonymous, block of code that can take arguments.
• Return a value and be assigned to a variable.
• A closure may reference variables declared in its surrounding scope.
• Can also contain free variables which are defined outside of its surrounding scope. In opposition to the formal definition of a closure.
Closures behind scenes• Groovy creates an inner anonymous class
• groovy.lang.Closure implements
• Cloneable
• Runnable
• GroovyCallable
• java.util.concurrent.Callable
• Serializable
Default methods
• Non-abstract method implementations to interfaces by utilizing the default keyword
• Also know as Extension Methods
Default methods
• Can be static methods
• We can use them to implement multiple inheritance
• Naming conflict¡
Traits
• The same idea as default methods in Java
• Interface with method bodies
• Can be stateful
• Multiple inheritance
java.util.Stream• 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 parallel.
Groovy GDK
• Groovy add lots of enhancements into the standard JDK
• Groovy “injects” additional methods on the standard Java libraries
• GDK adds syntactic sugar to Java APIs for better and easier usage.
GDK and Java 8
• Some GDK facilities will be deprecated in favor of Stream
• Stream is more efficient and performant
• Groovy will focus in better integration with Java 8 enhancements
java.util.Optional<T>• 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).
Groovy Truth
• Everything that’s null, empty, zero-sized, equal to zero is false. Otherwise is true
• Groovy Truth is quite simple: just implement a custom asBoolean() method.
What to do?
• IMO, Java 8 is a great release
• Groovy is a madure alternative language for the JVM
• My recommendation is to use both languages
Groovy Highlights• Traits
• Metaprogramming
• AST Transformations
• DSL authoring
• Android support
• @TypeChecked @CompileStatic
Picture credits• https://www.flickr.com/photos/eneas/3108310839/
• https://www.flickr.com/photos/eneas/6395912803/
• https://www.flickr.com/photos/80651083@N00/5641290457/
• https://www.flickr.com/photos/annuar_martinez/3282050044/
• https://www.flickr.com/photos/luismontemayor/2550701324/
• https://www.flickr.com/photos/guseher/2393646953/
• https://www.flickr.com/photos/annuar_martinez/2342099774/
• https://www.flickr.com/photos/luismontemayor/4306210583/
• https://www.flickr.com/photos/lastingimages/12262034856/
• https://www.flickr.com/photos/atejada/14169147706/
• https://www.flickr.com/photos/luismontemayor/3018556662/
• https://www.flickr.com/photos/eneas/2290156408/
• https://www.flickr.com/photos/122274109@N03/16154663524/
• https://www.flickr.com/photos/krynowekeine/2755263171/
• https://www.flickr.com/photos/nino_ary/2901716788/
• https://www.flickr.com/photos/eneas/5279761/
• https://www.flickr.com/photos/lastingimages/12373124983/
• https://www.flickr.com/photos/rmgg-photos/8503188042/
• https://www.flickr.com/photos/luajr/15127961825/
• https://www.flickr.com/photos/gtercero/8740734623/
• https://www.flickr.com/photos/egmzcp15/5237066174/
• https://www.flickr.com/photos/rouleau/109405487/
• https://www.flickr.com/photos/xavier333/2257501542/
• https://www.flickr.com/photos/pvcg/3087911579/
• https://www.flickr.com/photos/notocajrs/12785034584/
• https://www.flickr.com/photos/nahual_neo/8005123271/
• https://www.flickr.com/photos/chicotown/3189442699/
• https://www.flickr.com/photos/kootenayhulio/5497898774/
• https://www.flickr.com/photos/maybemaq/16579363326/
• https://www.flickr.com/photos/luajr/15484454837/
Picture credits• https://www.flickr.com/photos/nahual_neo/4290121443/
• https://www.flickr.com/photos/70120970@N08/8493760801/
• https://www.flickr.com/photos/gustavothomastheatre/15823470423/
• https://www.flickr.com/photos/graficris_lgs/15543907818/
• https://www.flickr.com/photos/gustavothomastheatre/15708898754/
• https://www.flickr.com/photos/john_colby/8430729849/
• https://www.flickr.com/photos/egmzcp15/4144937818/
• https://www.flickr.com/photos/10066932@N03/2552609658/
• https://www.flickr.com/photos/bdebaca/2987372965/
• https://www.flickr.com/photos/beco/23806123