Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Groovy & Java 8
A 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 capabilitie...
Seamlessly and transparently
integrates and interoperates with
Java and any third-party libraries
Java 8 matters
• Java 7 EOL. April 2015
• Java 6 < go figure.
• “Brand new” techniques, patterns, paradigms. Built in the l...
Java 8 features
• New Syntax
• Stream API
• Profiles
• JavaFX
• Date/Time API
• Nashorn
• Pack200
• JDBC
• Concurrency
• JA...
Java 8 syntax
• Lambda expressions
• Method references
• static & default methods in interfaces
• Repeating annotations
• ...
Comparison
Feature similarities
• Java 8
• Lambda expressions
• Default methods
• Optional types
• Stream API
• Method references
• G...
Code as data
• Interfaces with one method (functional interface)
• Single Abstract Method interfaces (SAM Interfaces)
• In...
Java 8 Lambdas
Lambda expressions
Groovy Closures
• Since 2003.
• Is an open, anonymous, block of code that can take
arguments.
• Return a value and be assi...
Closures in action
GDK (Groovy Dev Kit)
Closures behind scenes
• Groovy creates an inner anonymous class
• groovy.lang.Closure implements
• Cloneable
• Runnable
•...
Java 8 with Groovy
Java 8
Default methods
Default methods
• Non-abstract method implementations to
interfaces by utilizing the default keyword
• Also know as Extens...
Default methods
Default methods
• Can be static methods
• We can use them to implement multiple
inheritance
• Naming conflict¡
Groovy Traits
Traits
• The same idea as default methods in Java
• Interface with method bodies
• Can be stateful
• Multiple inheritance
Stream API
java.util.Stream
• Represents a sequence of elements on which one or
more operations can be performed.
• Stream operations...
Streams usage
Streams with
method references
Map-Reduce
GDK Collections
Groovy GDK
• Groovy add lots of enhancements into the
standard JDK
• Groovy “injects” additional methods on the
standard J...
Using GDK facilities
GDK and Java 8
• Some GDK facilities will be deprecated in favor
of Stream
• Stream is more efficient and performant
• Groo...
Java 8 Optionals
java.util.Optional<T>
• NPE is awkward.
• A optional is a container object which may or
may not contain a non-null value.
...
Optional usage
Stream/Optional
real life example
Groovy Truth
Groovy Truth
• Everything that’s null, empty, zero-sized, equal to
zero is false. Otherwise is true
• Groovy Truth is quit...
Null handling in Groovy
What to do?
• IMO, Java 8 is a great release
• Groovy is a madure alternative language for the
JVM
• My recommendation is ...
Java 8 highlights
• Lambdas
• Stream API
• Nashorn. Avatar project
Groovy Highlights
• Traits
• Metaprogramming
• AST Transformations
• DSL authoring
• Android support
• @TypeChecked @Compi...
Contact
• @domix
• domingo.suarez@gmail.com
• #JaverosMX
Picture credits
• https://www.flickr.com/photos/eneas/3108310839/
• https://www.flickr.com/photos/eneas/6395912803/
• https:...
Picture credits
• https://www.flickr.com/photos/nahual_neo/4290121443/
• https://www.flickr.com/photos/70120970@N08/84937608...
javerosmx-2015-marzo-groovy-java8-comparison
javerosmx-2015-marzo-groovy-java8-comparison
javerosmx-2015-marzo-groovy-java8-comparison
javerosmx-2015-marzo-groovy-java8-comparison
javerosmx-2015-marzo-groovy-java8-comparison
javerosmx-2015-marzo-groovy-java8-comparison
javerosmx-2015-marzo-groovy-java8-comparison
Upcoming SlideShare
Loading in …5
×

javerosmx-2015-marzo-groovy-java8-comparison

1,208 views

Published on

La idea de la charla es dar un comparación entre los diversos features de ambos lenguajes como:

* Lambdas/Closures
* Method references
* Default Methods / Traits
* Soporte de APIs de Java 8 en Groovy

Published in: Software
  • Be the first to comment

javerosmx-2015-marzo-groovy-java8-comparison

  1. 1. Groovy & Java 8 A very simple comparison @ JaverosMX, Mexico City 30/Marzo/2015Domingo Suárez Torres (@domix)
  2. 2. Agenda • Background • A little bit about Groovy • Why is important to upgrade to Java 8 • Comparison • Very small :’(
  3. 3. 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
  4. 4. Seamlessly and transparently integrates and interoperates with Java and any third-party libraries
  5. 5. 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.
  6. 6. Java 8 features • New Syntax • Stream API • Profiles • JavaFX • Date/Time API • Nashorn • Pack200 • JDBC • Concurrency • JAXP • Hotspot • Java Mission Control
  7. 7. Java 8 syntax • Lambda expressions • Method references • static & default methods in interfaces • Repeating annotations • Annotation on types • Improved type inference • Method parameter reflection
  8. 8. Comparison
  9. 9. Feature similarities • Java 8 • Lambda expressions • Default methods • Optional types • Stream API • Method references • Groovy • Closures • Traits • Truth & null handling • GDK Collections • Method closures
  10. 10. 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.
  11. 11. Java 8 Lambdas
  12. 12. Lambda expressions
  13. 13. 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.
  14. 14. Closures in action
  15. 15. GDK (Groovy Dev Kit)
  16. 16. Closures behind scenes • Groovy creates an inner anonymous class • groovy.lang.Closure implements • Cloneable • Runnable • GroovyCallable • java.util.concurrent.Callable • Serializable
  17. 17. Java 8 with Groovy
  18. 18. Java 8 Default methods
  19. 19. Default methods • Non-abstract method implementations to interfaces by utilizing the default keyword • Also know as Extension Methods
  20. 20. Default methods
  21. 21. Default methods • Can be static methods • We can use them to implement multiple inheritance • Naming conflict¡
  22. 22. Groovy Traits
  23. 23. Traits • The same idea as default methods in Java • Interface with method bodies • Can be stateful • Multiple inheritance
  24. 24. Stream API
  25. 25. 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.
  26. 26. Streams usage
  27. 27. Streams with method references
  28. 28. Map-Reduce
  29. 29. GDK Collections
  30. 30. 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.
  31. 31. Using GDK facilities
  32. 32. 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
  33. 33. Java 8 Optionals
  34. 34. 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).
  35. 35. Optional usage
  36. 36. Stream/Optional real life example
  37. 37. Groovy Truth
  38. 38. 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.
  39. 39. Null handling in Groovy
  40. 40. 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
  41. 41. Java 8 highlights • Lambdas • Stream API • Nashorn. Avatar project
  42. 42. Groovy Highlights • Traits • Metaprogramming • AST Transformations • DSL authoring • Android support • @TypeChecked @CompileStatic
  43. 43. Contact • @domix • domingo.suarez@gmail.com • #JaverosMX
  44. 44. 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/
  45. 45. 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

×