Java 8 new features

36
JAVA 8 18.3.2014 FinJUG - Petteri Hietavirta @FinJUG @pethie

description

Java 8 presentation for FinJUG. Covers new features like lambda, default methods and new date time.

Transcript of Java 8 new features

Page 1: Java 8 new features

JAVA 818.3.2014

FinJUG - Petteri Hietavirta

@FinJUG @pethie

Page 2: Java 8 new features

History• Timeline of Java

– JDK 1.0 1996

– JDK 1.1 1997

– J2SE 1.2 1998

– J2SE 1.3 2000

– J2SE 1.4 2002

– J2SE 5.0 2004

– Java SE 6 2006

– Java SE 7 2011

– Java SE 8 2014 – General Availibility today!

Page 3: Java 8 new features

Why such a long wait

• Security improvements

Page 4: Java 8 new features

New features• Lambda Expressions & Virtual Extension Methods

• Autoconf-Based Build System

• Lambda-Form Representation for Method Handles

• Compact Profiles

• Prepare for Modularization

• Leverage CPU Instructions for AES Cryptography

• Nashorn JavaScript Engine

• Mechanical Checking of Caller-Sensitive Methods

• Document JDK API Support and Stability

• Reduce Cache Contention on Specified Fields

Page 5: Java 8 new features

Gc / runtime

• Remove the Permanent Generation

• Retire Some Rarely-Used GC Combinations

• Enhanced Verification Errors

• Reduce Class Metadata Footprint

• Small VM

• Fence Intrinsics

Page 6: Java 8 new features

core / lang• Launch JavaFX Applications

• Generalized Target-Type Inference

• Annotations on Java Types

• DocTree API

• Add Javadoc to javax.tools

• Remove the Annotation-Processing Tool (apt)

• Access to Parameter Names at Runtime

• Repeating Annotations

• Enhance javac to Improve Build Speed

• DocLint

Page 7: Java 8 new features

libs

• Parallel Array Sorting

• Bulk Data Operations for Collections

• Enhance Core Libraries with Lambda

• Charset Implementation Improvements

• javax.lang.model Implementation Backed by Core Reflection

• Base64 Encoding & Decoding

• Reduce Core-Library Memory Usage

• Date & Time API

• Concurrency Updates

• JDBC 4.2

• Optimize java.text.DecimalFormat.format

• Statically-Linked JNI Libraries

• Handle Frequent HashMap Collisions with Balanced Trees

Page 8: Java 8 new features

i18n

• Improve Locale Data Packaging and

Adopt Unicode CLDR Data

• BCP 47 Locale Matching

• Unicode 6.2

Page 9: Java 8 new features

security• HTTP URL Permissions

• MS-SFU Kerberos 5 Extensions

• TLS Server Name Indication (SNI) Extension

• AEAD CipherSuites

• Stronger Algorithms for Password-Based Encryption

• Configurable Secure Random-Number Generation

• Enhance the Certificate Revocation-Checking API

• NSA Suite B Cryptographic Algorithms

• SHA-224 Message Digests

• PKCS#11 Crypto Provider for 64-bit Windows

• Limited doPrivileged

• Overhaul JKS-JCEKS-PKCS12 Keystores

• JAXP 1.5: Restrict Fetching of External Resources

Page 10: Java 8 new features

Highlights

• Default methods

• Lambda, Stream API

• DateTime

Page 11: Java 8 new features

Default methods

public interface Demo {

public void foo();

}

class DemoImpl implements Demo {

public void foo() {

}

}

Page 12: Java 8 new features

public interface Demo {

public void foo();

public void bar();

}

class DemoImpl implements Demo {

public void foo() {

}

public void bar() {

….

}

}

Page 13: Java 8 new features

public interface Demo {

public void foo();

default public void bar() {

}

}

class DemoImpl implements Demo {

public void foo() {

}

}

Page 14: Java 8 new features

λ Lambda

• Lambda expressions are

computational units that operate on

some data (input) and produce some

data (output)

• Cleaner code, better suited for

parallel and concurrent programming

Page 15: Java 8 new features

SyntaxArgument list Arrow token Body

(int x, int y) -> x+y

(int x, int y) -> x + y

() -> 42

(String s) -> { System.out.println(s); }

n -> System.out.println(n)System.out :: println // method reference

Page 16: Java 8 new features

Runnable

new Thread(new Runnable() {

@Override public void run({

System.out.println(”verbose");

}

}).start();

new Thread( () ->

System.out.println(”simple") ).start();

Page 17: Java 8 new features

• Single Abstract Method / functional

interfaceinterface IntegerMath{ int operation(int a,int b); }

Usage:

IntegerMath addition = (a,b) -> a+bIntegerMath multi = (a,b) -> a*b;System.out.println("Add lambda :"+ addition.operation(10, 4));System.out.println("Multiply lambda :"+ multi.operation(10, 4));

Page 18: Java 8 new features

Foreach

List names =

Arrays.asList("Java", "Joe",

"Sam");

names.forEach(n ->

System.out.println(n));

Page 19: Java 8 new features

java.util.functional

public interface Predicate<T>{

boolean test(T t);

}

Predicate<String> startsWithJ = (n) ->

n.startsWith("J");

Page 20: Java 8 new features

Stream API - filter

names.stream()

.filter(startsWithJ)

.forEach((n) ->

System.out.println(n))

Page 21: Java 8 new features

map

names.stream()

.map((name) -> name.toUpperCase())

.collect(Collectors.toList())

Page 22: Java 8 new features

reduce

int sum = 0;

list.forEach(e -> {

sum += e.size();

});

int sum = Stream.of(1,2,3,4)

.reduce(0,(a, b) -> a + b);

Page 23: Java 8 new features

parallel

names.parallelStream()

.filter(…

Page 24: Java 8 new features

Things to watch out

• Non-final variable capture

• Handling checked exceptions is pain

• Control flow – no break, no early

return

• Nasty to debug in parallel context

• Benchmark

Page 25: Java 8 new features

JSR-310 Date Time API

• Build on experiences from Jodatime

• java.time

• Based on nanoseconds

• Clear, fluent, immutable, extensible

Page 26: Java 8 new features

Immutable

LocalDate dateOfBirth =

LocalDate.of(2012, Month.MAY, 14);

LocalDate firstBirthday =

dateOfBirth.plusYears(1);

Page 27: Java 8 new features

Adjusters

• Previous Thursday of any date

date.with(TemporalAdjuster.previous

(DayOfWeek.THURSDAY)));

Page 28: Java 8 new features

Payday

Twice a month, 15th and the last day. If

day is Sat or Sun, then happens on

previous Fri

LocalDate nextPayday =

date.with(new PaydayAdjuster());

Page 29: Java 8 new features

public Temporal adjustInto(Temporal input) {

LocalDate date = LocalDate.from(input);

int day;

if (date.getDayOfMonth() < 15) {

day = 15;

} else {

day =

date.with(TemporalAdjusters.lastDayOfMonth()).getDayOfMonth();

}

date = date.withDayOfMonth(day);

if (date.getDayOfWeek() == DayOfWeek.SATURDAY ||

date.getDayOfWeek() == DayOfWeek.SUNDAY) {

date = date.with(TemporalAdjusters.previous(DayOfWeek.FRIDAY));

}

return input.with(date);

}

Page 30: Java 8 new features

Period

LocalDate today = LocalDate.now();

LocalDate birthday = LocalDate.of(1960, Month.JANUARY, 1);

Period p = Period.between(birthday, today);

long p2 = ChronoUnit.DAYS.between(birthday, today);

System.out.println(

"You are " + p.getYears() + " years, "

+ p.getMonths() + " months, and "

+ p.getDays() + " days old. (" + p2 + " days total)");

Page 31: Java 8 new features

Compact profiles

10Mb17Mb24Mb140Mb

Page 32: Java 8 new features

Missing from Java 8

• Jigsaw

• Java 9 coming – maybe 2016?

Page 33: Java 8 new features

IDE support

• IntelliJ IDEA 12 & 13

• Eclipse Kepler (4.3) & Luna (4.4)

• Netbeans 7.4+

Page 36: Java 8 new features

Thank you!