27
JDK8 The New Features

JDK 8

Embed Size (px)

DESCRIPTION

What's new in latest Java version 8

Citation preview

Page 1: JDK 8

JDK8The New Features

Page 2: JDK 8

Language

Page 3: JDK 8

Lambdas and functional interfaces

new Thread(new Runnable() { @Override public void run() { System.out.println("Hello from thread"); }}).run();

new Thread(() -> System.out.println("Hello from thread")).run()

@FunctionalInterfacepublic interface Runnable { public abstract void run();}

Page 4: JDK 8

Lambdas and functional interfaces

Arrays.asList("a", "b", "d").sort((e1, e2) -> e1.compareTo(e2));

Arrays.asList("a", "b", "d").sort( (String e1, String e2) -> e1.compareTo(e2));

Arrays.asList("a", "b", "d").sort( (e1, e2) -> { int result = e1.compareTo(e2); return result; });

Page 5: JDK 8

Default methods

interface DefaultInterface { void show(); default void display() { System.out.println("Displaying something"); }}

But … why ??

Page 6: JDK 8

Default methods

package java.lang;public interface Iterable<T> { void forEach(Consumer<? super T> action);}

Core interfaces have new methods

This breaks backwards compatibility

package java.lang;public interface Iterable<T> { default void forEach(Consumer<? super T> action) { for (T t : this) { action.accept(t); } }}

This doesn’t

Page 7: JDK 8

Static interface methods

interface MyInterface { void method1(); static void method2() { System.out.println("Hello from

static"); }}...MyInterface.method2();

Keep interface related helper methods in the same class rather than creating a new one

Page 8: JDK 8

Method referencesStatic reference

class Example { public static void main(String[] args) { String[] str = {"one", "two", "3", "four"}; Arrays.sort(str, Example::compareByLength); Arrays.sort(str, (s1, s2) -> s1.length() - s2.length()); } public static int compareByLength(String s1, String s2) { return s1.length() - s2.length(); }}

Page 9: JDK 8

Method referencesInstance reference

@FunctionalInterface // New JDK8 interfacepublic interface Supplier { T get();}

public String function(Supplier<String> supplier) { return supplier.get();}

public void example() { final String x = "A string"; function(() -> x.toString()); function(x::toString);}

Page 10: JDK 8

Method referencesConstructor reference

class Car {}

class Example { public static Car createCar(Supplier supplier) { return supplier.get(); } public static void repair(Car car) {} public static void main(String[] args) { Car car = createCar(Car::new); List cars = Arrays.asList(car); cars.forEach(Example::repair); }}

Page 11: JDK 8

Repeating annotations

class RepeatingAnnotations {

public @interface Filters { // A hidden filter holder Filter[] value(); } @Repeatable(Filters.class) public @interface Filter { String value(); } @Filter("filter1") @Filter("filter2") public void filterredMethod() {}}

Repeat yourself

Page 12: JDK 8

Extended annotations

class Annotations { public @interface NotEmpty {} public static class Holder<@NonEmpty T> extends @NonEmpty Object{ public void method() throws @NonEmpty Exception {} } public static void main(String[] args) { final Holder<String> holder = new @NonEmpty Holder<String>(); @NonEmpty Collection<@NonEmpty String> strings = new ArrayList<>(); } }

Annotate anything

Page 13: JDK 8

Libraries

Page 14: JDK 8

Optional

Optional<String> name = Optional.ofNullable(null);System.out.println("Name is set? " + name.isPresent() ); System.out.println("Name: " + name.orElseGet( () -> "[none]" ));System.out.println(name.map( s -> "Hey " + s + "!" ) .orElse("Hey Stranger!"));

java.util.Optional

Name is set? falseName: [none]Hey Stranger!

Page 15: JDK 8

Streams

for (Student student : students) {if (student.getName().startsWith("A")){

names.add(student.getName());}

}

java.util.stream

List<string> names = students.stream().map(Student::getName).filter(name -> name.startsWith("A")).collect(Collectors.toList());

The old way

The Java8 way

Page 16: JDK 8

Streamsjava.util.stream

int sum = students.parallelStream().map(Student::getName).filter(name -> name.startsWith("A")).mapToInt(String::length) .sum();

System.out.println(Arrays.asList("One", "Two", "Three")

.stream().collect(Collectors.groupingBy(String::length)));

{3=[One, Two], 5=[Three]}

Page 17: JDK 8

Date/Time APIJSR 310 - java.time

Page 18: JDK 8

Date/Time APIJSR 310 - java.time

Calendar cal = Calendar.getInstance();cal.set(Calendar.HOUR, cal.get(Calendar.HOUR) + 2);

LocalTime now = LocalTime.now();LocalTime later = now.plus(2, HOURS);

The old way

The Java8 way

Page 19: JDK 8

NashornJavaScript engine

• Rhino replacement

• Faster (2 to 10x performance boost)

• Full ECMAScript 5.1 support + extensions

• Compiles JS to Java bytecode

Page 20: JDK 8

NashornExample

ScriptEngineManager manager = new ScriptEngineManager();ScriptEngine engine = manager.getEngineByName("JavaScript");

System.out.println(engine.getClass().getName() );System.out.println("Result:" + engine.eval(

"function f() { return 1; }; f() + 1;" ) );

jdk.nashorn.api.scripting.NashornScriptEngineResult: 2

Page 21: JDK 8

NashornPerformance

V8Chrome 31 SpidermonkeyFF 25.0.1

Nashorn

Octane 2.0 14 474 10 066 9 307

Sunspider 1.0.2 220.8 ms 246.5 ms 256.2 ms

http://wnameless.wordpress.com/2013/12/10/javascript-engine-benchmarks-nashorn-vs-v8-vs-spidermonkey/

Page 22: JDK 8

Base64Finally … java.util.Base64

final String encoded = Base64 .getEncoder() .encodeToString( text.getBytes( StandardCharsets.UTF_8 ));

final String decoded = new String(Base64.getDecoder().decode(encoded),StandardCharsets.UTF_8);

Page 23: JDK 8

Tools

Page 24: JDK 8

jjs

function f() {return 1;

};print(f() + 1);

jjs file.js

2

file.js

command

output

Nashorn runner

Page 25: JDK 8

jdeptsDependency analyzer

jdeps jdom2-2.0.5.jar

jdom2-2.0.5.jar -> /Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home/jre/lib/rt.jar org.jdom2 (jdom2-2.0.5.jar) -> java.io -> java.lang -> java.net -> java.util -> java.util.concurrent org.jdom2.adapters (jdom2-2.0.5.jar) -> java.lang -> java.lang.reflect -> javax.xml.parsers…

command

output

Page 26: JDK 8

JVM

Metaspace

Page 27: JDK 8

Thank you :)