12
Major New Language Features Neil Brown 10/2014 @neiljbrown neiljbrown.com

2014 10 java 8 major new language features

Embed Size (px)

DESCRIPTION

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

Citation preview

Page 1: 2014 10 java 8 major new language features

Major New Language Features

Neil Brown 10/2014@neiljbrown neiljbrown.com

Page 2: 2014 10 java 8 major new language features

Overview of Major New Features

● Lambda expressions● Streams (aka bulk / aggregate data ops)● Default methods● Date/time API● Optional type

Page 3: 2014 10 java 8 major new language features

Lambda Expressions

Anonymous function, with no classExpression syntax like that of a method(String s1, String s2) -> s1.length() - s2.length();

Assign to variables or pass as method paramBenefits

● Another means of code reuse● Terser implementation of single abstract methods (SAMs)

Page 4: 2014 10 java 8 major new language features

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 operationsConcept:

● Convert source to stream● Process elements in parallel, concurrently● Collect resulting elements

Page 5: 2014 10 java 8 major new language features

Streams API

Sources include Collections, Array, I/O classesBenefits● 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());

Page 6: 2014 10 java 8 major new language features

Default Methods

Interfaces can include default implementationsinterface Logger {

default void info(String message) {

Logger.log("[INFO]", message);

} ...}

Compiler uses the ‘default method’ in classes which don’t provide own implementation

Page 7: 2014 10 java 8 major new language features

Default Methods

Interfaces still not permitted to have stateBenefit

● Java APIs can evolve without breaking backwards compatibility eg add functional methods to Collection interfaces for Streams

Page 8: 2014 10 java 8 major new language features

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

Page 9: 2014 10 java 8 major new language features

java.util.Optional

New type for value that may be nullUsed as method return type rather than null eg Optional<Soundcard> optionalSoundcard = computer.getSoundcard();

Forces client to deal with null at compile-timeProvides convenience methods ifPresent(), orElse() for simpler handling of possible nullProvides null-safe functional methods filter(), map() etc

Page 10: 2014 10 java 8 major new language features

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

Page 11: 2014 10 java 8 major new language features

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

Page 12: 2014 10 java 8 major new language features

Questions?

Thanks for attendingNeil Brown

@neiljbrown neiljbrown.com