O slideshow foi denunciado.
Utilizamos seu perfil e dados de atividades no LinkedIn para personalizar e exibir anúncios mais relevantes. Altere suas preferências de anúncios quando desejar.

2014 10 java 8 major new language features

463 visualizações

Publicada em

Overview of major new language features in Java 8, with code examples.

Publicada em: Software
  • Entre para ver os comentários

2014 10 java 8 major new language features

  1. 1. Major New Language Features Neil Brown 10/2014 @neiljbrown neiljbrown.com
  2. 2. Overview of Major New Features ● Lambda expressions ● Streams (aka bulk / aggregate data ops) ● Default methods ● Date/time API ● Optional type
  3. 3. Lambda Expressions Anonymous function, with no class Expression syntax like that of a method (String s1, String s2) -> s1.length() - s2.length(); Assign to variables or pass as method param Benefits ● Another means of code reuse ● Terser implementation of single abstract methods (SAMs)
  4. 4. Streams API APIs support aggregating ‘streams’ on-the-fly, concurrently, using functional methods eg filter ‘Stream’ - seq. of elements from a source supporting aggregating (‘bulk’) data operations Concept: ● Convert source to stream ● Process elements in parallel, concurrently ● Collect resulting elements
  5. 5. Streams API Sources include Collections, Array, I/O classes Benefits ● Better performance via lazy evaluation of methods on stream and parallel processing ● Replace boilerplate external iteration code eg List<Student> students = new ArrayList<>(100); ... List<Student> filteredStudents = students.stream() .filter(s -> s.getDob().getYear() > yearOfBirthFilter) .collect(Collectors.toList());
  6. 6. Default Methods Interfaces can include default implementations interface Logger { default void info(String message) { Logger.log("[INFO]", message); } ... } Compiler uses the ‘default method’ in classes which don’t provide own implementation
  7. 7. Default Methods Interfaces still not permitted to have state Benefit ● Java APIs can evolve without breaking backwards compatibility eg add functional methods to Collection interfaces for Streams
  8. 8. Date & Time API java.time.* - Addresses issues with java.util.Date and Calendar ● Thread-safe. Immutable classes. ● Consistent design, inspired by Joda-Time ● Better time-zone support Fluent API; Major classes: ● LocalDateTime and ZonedDateTime - Factory methods, parsing, truncation ● Period and Duration - lengths of time
  9. 9. java.util.Optional New type for value that may be null Used as method return type rather than null eg Optional<Soundcard> optionalSoundcard = computer.getSoundcard(); Forces client to deal with null at compile-time Provides convenience methods ifPresent(), orElse() for simpler handling of possible null Provides null-safe functional methods filter(), map() etc
  10. 10. java.util.Optional Benefits ● More expressive / explicit API methods ● Reduces no. of NullPointerException ● Provided null safe methods reduce need for nested conditional statements to handle null refs
  11. 11. Code Examples Code examples for each new feature ● https://bitbucket.org/neilbrown - ‘java8- examples’ repository Includes pre Java 8 equivalent code to help identify eligible cases
  12. 12. Questions? Thanks for attending Neil Brown @neiljbrown neiljbrown.com

×