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.

Java 7 & 8 - A&BP CC

322 views

Published on

In this workshop we would like to take a closer look at the new features of Java 7 and Java 8.

Java 7 has brought a couple of language enhancements to assist developers in their daily programming tasks and starting from Java 8 we finally have the opportunity to include a functional programming touch to Java thanks to Lambdas! Lambdas allow us to specify closures to create clean and brief solutions for which we would otherwise be obligated to write down a lot of boilerplate code.

For example in order to sort a List of Strings based on their length we can now write the following code:
Collections.sort(list, (s1, s2) -> s1.length() - s2.length());
Besides lambdas, Java 8 also comes with extension methods, allowing us to write interfaces containing implemented methods, streams and bulk data operations, granting us the opportunity to chain operations on collections to obtain a filtered or transformed set of the elements in our collection, and the possibility to pass functions as parameters. These features alone will have a major impact on the way how Java APIs can be written. There's also a brand new Date and Time API inspired on the popular JodaTime API and a couple of other language enhancements to aid Java developers in writing solutions in a more elegant way.

Be sure to follow this workshop to stay up to date with the latest evolutions of the Java programming language. Java 8 is definitely an update of the programming language which you don't want to miss out on!

Published in: Education
  • Be the first to comment

Java 7 & 8 - A&BP CC

  1. 1. 1 Java 7 & 8 Yannick De Turck Ken Coenen
  2. 2. 2 Java 7 & 8  18h15 – Java 7 (Ken)  18h45 – Q&A  19h – Java 8 (Yannick)  19h30 – Q&A  19h45 – Exercises  20h30 – Solutions  21h - Finish
  3. 3. 3 Java 7 (July 28th 2011)  String in switch-statement  Automatic resource management  Diamond syntax  Better Exception handling with multi-catch  Literal enhancements  New IO API  Fork Join Framework  JVM enhancements
  4. 4. 4 Java 7: String in switch-statement
  5. 5. 5 Java 7: Automatic resource management  Try-with-resources statement - Resources are automatically closed  New AutoCloseable interface available to implement for your own classes
  6. 6. 6 Java 7: Automatic resource management
  7. 7. 7 Java 7: Diamond syntax  Type Inference for Generic Instance Creation  No longer required to repeat the type when instantiation
  8. 8. 8 Java 7: Better Exception handling with multi-catch  No longer one Exception per catch limit
  9. 9. 9 Java 7: Better Exception handling with multi-catch  Precise rethrowing
  10. 10. 10 Java 7: Literal enhancements  Prefix binary literals with 0b or 0B  Use underscores in your number literals to increase readability
  11. 11. 11 Java 7: New IO API  A whole new package: java.nio  Non-blocking IO  Buffer oriented instead of Stream oriented  New classes to improve working with files - Files - Path - FileSystem - WatchService - FileVisitor - ...
  12. 12. 12 Java 7: New IO API
  13. 13. 13 Java 7: New IO API
  14. 14. 14 Java 7: New IO API
  15. 15. 15 Java 7: Fork Join Framework  Parallel programming - Divide a process into smaller tasks via recursion which are handled by a processor - Combine the processed pieces into one result - Divide & conquer
  16. 16. 16 Java 7: Fork Join Framework  Extend RecursiveAction or RecursiveTasks if (my portion of the work is small enough) { do the work directly } else { split my work into two pieces invoke the two pieces and wait for the results }  Practical example: ForkBlur.java
  17. 17. 17 Java 7: JVM enhancements  Support for dynamically typed languages - Introduction of invokedynamic  A new bytecode instruction on the JVM for method invocation  http://niklasschlimm.blogspot.be/2012/02/java-7-complete-invokedynamic- example.html - Performance improvements for other languages living in the JVM such as Ruby, Groovy, …  Garbage-First Collector (or G1 collector) - Will eventually replace the Concurrent Mark-Sweep Collector (CMS) - Advantages: works with regions, more predictable
  18. 18. 18 Java 8 (March 18th 2014)  Lambda Expressions  Extension Methods  Functional Interfaces  Method and Constructor References  Streams and Bulk Data Operations for Collections  Removal of PermGen  New Date & Time API  New Default API for Base 64 Encoding  Improvements for Annotations  General Performance Improvements
  19. 19. 19 Java 8: Lambda Expressions  Allows writing code in a functional style  Passing behaviour to a method  Prior to Java 8: Anonymous Inner Class  Java 8: Lambda Expressions  Gets rid of boiler plate code  More readable and clear code  Type of param may be specified but isn’t obligated (params) -> expression () -> System.out.println(“Hello world!”); myButton.addActionListener () -> { doThis(); doThat(); }((e) -> println(“Clicked!));
  20. 20. 20 Java 8: Lambda Expressions
  21. 21. 21 Java 8: Extension Methods  Add non-abstract method implementations to interfaces using the ‘default’ keyword  But what happens if default methods collide when using multiple interfaces?
  22. 22. 22 Java 8: Extension Methods  Override method and pick the right implementation
  23. 23. 23 Java 8: Functional Interfaces  @FunctionalInterface  An interface with exactly one abstract method  Lambda expression is applicable as implementation  Build-in Functional Interfaces (java.util.function) - Predicate<T>: boolean test(T t); - Function<T>: R apply(T t); - Supplier<T>: T get(); - Consumer<T>: void accept(T t); - Comparator<T>: int compare(T o1, T o2);
  24. 24. 24 Java 8: Functional Interfaces
  25. 25. 25 Java 8: Method and Constructor References  Pass references of methods or constructors using the :: keyword  Useful in combination with the Predicate class  Bit shorter compared to lambdas ContainingClass::staticMethodName ContainingObject::instanceMethodName ContainingType::methodName ClassName::new String::valueOf s::toString String::toString String::new
  26. 26. 26 Java 8: Method and Constructor References
  27. 27. 27 Java 8: Method and Constructor References
  28. 28. 28 Java 8: Streams and Bulk Data Operations for Collections  java.util.Stream  A sequence of elements on which one or more operations can be performed  Intermediate vs terminal operation - Intermediate: returns the stream itself in order to be able to chain operations - Terminal: returns a result of a certain type  Streams are created on a source such as a java.util.Collection  Can be executed sequential or parallel  Parallel utilises Fork-Join - Watch out with long-running tasks! Blocks threads in the pool
  29. 29. 29 Java 8: Streams and Bulk Data Operations for Collections
  30. 30. 30 Java 8: Streams and Bulk Data Operations for Collections  Maps - Don’t support streams :-( - … But they now support various new and useful methods for executing common tasks!  V putIfAbsent(K key, V value)  void forEach(BiConsumer<? super K,? super V> action)  V computeIfPresent(K key, BiFunction<? super K,? super V,? extends V> remappingFunction)  V computeIfAbsent(K key, Function<? super K,? extends V> mappingFunction)  V getOrDefault(Object key, V defaultValue)  ...
  31. 31. 31 Java 8: Streams and Bulk Data Operations for Collections
  32. 32. 32 Java 8: Streams and Bulk Data Operations for Collections  Optional<T> - May or may not contain a non-null value - Avoid working with null (no NPEs!)
  33. 33. 33 Java 8: Removal of PermGen  PermGen memory space completely removed - PermSize and MaxPermSize JVM arguments are ignored and a warning gets displayed  Gets replaced by Metaspace - XX:MaxMetaspaceSize flag, default is unlimited - System memory is the limit instead of the fixed size at startup of PermGen - Metaspace will dynamically resize depending on demand at runtime  Note that this does not magically fixes your memory leaks!
  34. 34. 34 Java 8: New Date & Time API  Inspired by Joda Time - Human time vs computer time (aka millis since epoch)  Offers a solution to the sometimes cumbersome way of calculating dates and time  Interesting new classes: - Clock - ZoneId - LocalDate (date without timezone) - LocalTime (time without timezone) - LocalDateTime (datetime without timezone) - DateTimeFormatter - …
  35. 35. 35 Java 8: New Date & Time API
  36. 36. 36 Java 8: New Default API for Base64 Encoding  More extensive API than the 1.6+ Base64 API (sun.misc.BASE64Encoder)  3 encoders and decoders - Basic (For regular encoding) - URL (Encoded String needs to be used in file or url) - MIME (MIME friendly encoding)
  37. 37. 37 Java 8: New Default API for Base64 Encoding
  38. 38. 38 Java 8: Improvements for Annotations  Annotations in Java 8 are repeatable  @Repeatable
  39. 39. 39 Java 8: Improvements for Annotations
  40. 40. 40 Java 8: General Performance Improvements  Performs a bit faster compared to Java 7  Great performance improvement when making use of parallelism  Example with Arrays.sort
  41. 41. 41 Sources  https://github.com/yannickdeturck/workshop-java-7-8 (or shorter url: http://tinyurl.com/lzy56ng)  Java 8 Cheatsheet: http://www.java8.org
  42. 42. 42 Questions ?

×