Java World, Java Trends, Java 8 and Beyond (iForum - 2014)

40
Java Java World, Java Trends, Java 8 and Beyond Olena Syrota @osyrota

Transcript of Java World, Java Trends, Java 8 and Beyond (iForum - 2014)

Page 1: Java World, Java Trends, Java 8 and Beyond (iForum - 2014)

JavaJava World, Java Trends, Java 8 and Beyond Olena Syrota

@osyrota

Page 2: Java World, Java Trends, Java 8 and Beyond (iForum - 2014)

2

Agenda

▪ Java Today

▪ Java Trends

▪ Java 8

▪ Beyond Java 8

Page 3: Java World, Java Trends, Java 8 and Beyond (iForum - 2014)

3

Java Today

Page 4: Java World, Java Trends, Java 8 and Beyond (iForum - 2014)

4

Java Birthday

▪ Started in 1991

▪ Released in 1995

Who of you was born in 1991?

Page 5: Java World, Java Trends, Java 8 and Beyond (iForum - 2014)

5

Page 6: Java World, Java Trends, Java 8 and Beyond (iForum - 2014)

6

Java Today - Advantages

▪ Stable

▪ High performance

▪ Backward compatibility

▪ JVM

▪ Mature culture of build tools and practice

▪ JCP process and community involvement

▪ etc

Page 7: Java World, Java Trends, Java 8 and Beyond (iForum - 2014)

7

Java Today - Disadvantage

▪ A lot of boilerplate code (imperative programming style)

▪ Every language has lambda

Page 8: Java World, Java Trends, Java 8 and Beyond (iForum - 2014)

8

Trends

▪ Polyglot development

▪ Languages over JVM (Scala, Groovy, Clojure etc)

▪ Reduce boilerplate code

▪ Java 8 - Lambdas

▪ Maximum using of checking code conditions at compile time

▪ Lambda 8 - Typed annotations

▪ Internet of Things

▪ Java on Raspberry Pi

Page 9: Java World, Java Trends, Java 8 and Beyond (iForum - 2014)

9

Polyglot Programming

Page 10: Java World, Java Trends, Java 8 and Beyond (iForum - 2014)

10

Java 8 Released 18 March 2014

▪ 55 new features in Java SE 8

Page 11: Java World, Java Trends, Java 8 and Beyond (iForum - 2014)

11

Java SE 8 “Umbrella” Specification JSR 337

▪ https://www.jcp.org/en/jsr/detail?id=337

▪ Expert Group

▪ Kevin Bourrillion (Google)

▪ Andrew Haley (Red Hat)

▪ Steve Poole (IBM)

▪ Mark Reinhold (Oracle)

Page 12: Java World, Java Trends, Java 8 and Beyond (iForum - 2014)

12

Java 8

Java for Everyone• Profiles for constrained devices• JSR 310 - Date & Time APIs• Non-Gregorian calendars• Unicode 6.2• ResourceBundle. • BCP47 locale matching

• Globalization & Accessibility

Innovation• Lambda aka Closures• Language Interop• Nashorn

Core Libraries• Parallel operations for core

collections APIs• Improvements in functionality• Improved type inference

Security• Limited doPrivilege• NSA Suite B algorithm support• SNI Server Side support• DSA updated to FIPS186-3• AEAD JSSE CipherSuites

Tools• Compiler control & logging• JSR 308 - Annotations on

Java Type• Native app bundling • App Store Bundling tools

Client•Deployment enhancements•JavaFX 8•Java SE Embedded support•Enhanced HTML5 support•3D shapes and attributes•Printing

General Goodness• JVM enhancements• No PermGen limitations• Performance lmprovements

Page 13: Java World, Java Trends, Java 8 and Beyond (iForum - 2014)

13

Java 8

Page 14: Java World, Java Trends, Java 8 and Beyond (iForum - 2014)

14

Java 8 Features to Discuss

▪ Lambda

▪ Bulk operations for collections

▪ Parallel operations on collections

▪ Date and Time API

▪ Annotations on Types

▪ Nashorn

Page 15: Java World, Java Trends, Java 8 and Beyond (iForum - 2014)

15

Lambdas

▪ The most significant changes and innovation in Java

▪ Lambda (closures)

▪ Interface evolution (functional interfaces and default methods)

▪ Evolution of Collections library (bulk operations)

▪ Simplified syntax of parallel computation with libraries

Page 16: Java World, Java Trends, Java 8 and Beyond (iForum - 2014)

16

Lambdas

( ) -> { }

Page 17: Java World, Java Trends, Java 8 and Beyond (iForum - 2014)

17

Lambda Sample

interface Adder {

int add (int a, int b);

}

Adder adder = (a, b)->a+b;

Page 18: Java World, Java Trends, Java 8 and Beyond (iForum - 2014)

18

Impact of Lambda

▪ Passing behavior as parameter has great impact on code

▪ Control is transferred from client to library

Page 19: Java World, Java Trends, Java 8 and Beyond (iForum - 2014)

19

Internal Iteration

public void internalIteration(List<Person> voters) {

voters.forEach(v->System.out.println(v));

}

Page 20: Java World, Java Trends, Java 8 and Beyond (iForum - 2014)

20

Parallel Internal Iteration

public void internalIteration(List<Person> voters) {

voters.parallelStream().forEach(v->System.out.println(v));

}

Page 21: Java World, Java Trends, Java 8 and Beyond (iForum - 2014)

21

Bulk Operations on Collections

public static List<Person> eligibleVoters(List<Person> potentialVoters,

int legalAgeOfVoting) {

return potentialVoters

.stream()

.filter(s->s.getAge()>=legalAgeOfVoting)

.collect(Collectors.toList());

}

Page 22: Java World, Java Trends, Java 8 and Beyond (iForum - 2014)

22

More Bulk Operations

public static double averageAgeOfVoters(List<Person> voters) {

return voters

.stream()

.mapToInt(v->v.getAge())

.average()

.getAsDouble();

}

Page 23: Java World, Java Trends, Java 8 and Beyond (iForum - 2014)

23

Sequential version

public static Set<Ballot> unspoiledBallots(Set<Ballot> ballots) {

return ballots

.stream()

.filter(b->!b.isSpoiled())

.collect(Collectors.toSet());

}

Page 24: Java World, Java Trends, Java 8 and Beyond (iForum - 2014)

24

Parallel version

public static Set<Ballot> unspoiledBallots(Set<Ballot> ballots) {

return ballots

.parallelStream()

.filter(b->!b.isSpoiled())

.collect(Collectors.toSet());

}

Page 25: Java World, Java Trends, Java 8 and Beyond (iForum - 2014)

25

Parallel Computations in Java:form Thread to Project Lambda

Page 26: Java World, Java Trends, Java 8 and Beyond (iForum - 2014)

26

Interface Evolution

▪ Functional interfaces

▪ Default methods

Page 27: Java World, Java Trends, Java 8 and Beyond (iForum - 2014)

27

Interface Evolution

▪ Standard interface Collection has new method forEach

Default method – new feature of language.

Virtual method can have default implementation.

This allows to transfer control over iteration to library.

interface Collection<T> extends Iterable<T> { default void forEach (Block<T> action) { for (T t: this) { action.apply(t); } } }

Page 28: Java World, Java Trends, Java 8 and Beyond (iForum - 2014)

28

Interface Evolution

Functional interface – allows to pass behavior as parameter

interface Collection<T> extends Iterable<T> { default void forEach (Block<T> action) { for (T t: this) { action.apply(t); } } }

@FunctionalInterface interface Block<T> { void apply(T t); }

Page 29: Java World, Java Trends, Java 8 and Beyond (iForum - 2014)

29

Java 8 Added Plenty of New Features

▪ Java 8 is biggest update to java programming model

▪ Small set of features, with a big impact

▪ Lambda expressions

▪ Default methods

▪ Bulk operations on collections

▪ Coordinated co-evolution of

▪ language

▪ libraries

▪ APIs

Page 30: Java World, Java Trends, Java 8 and Beyond (iForum - 2014)

30

Date and Time API

▪ Why new Date and Time API?

▪ Immutable-value classes ( immutable => thread-safe)

▪ Domain-driven API design

Page 31: Java World, Java Trends, Java 8 and Beyond (iForum - 2014)

31

Date and Time API Sample

public static Period periodBetweenIFormAndLongestDay() {

LocalDate iForumDay = LocalDate.of(2014, 04, 24);

LocalDate longestDay = iForumDay.with(Month.JUNE).withDayOfMonth(22);

return Period.between(longestDay, iForumDay);

}

Page 32: Java World, Java Trends, Java 8 and Beyond (iForum - 2014)

32

Annotations on Java Types

▪ Before the Java SE 8 release, annotations could only be applied to declarations (class, constructor, method, field, local variable, parameter etc.)

▪ As of the Java SE 8 release, annotations can also be applied to any type use (e.g. generic type arguments, new, casts, implements, throws)

Map<@NonNull String, @NonEmpty List<Document>>

files;

myString = (@NonNull String) myObject;

Page 33: Java World, Java Trends, Java 8 and Beyond (iForum - 2014)

33

Error Detection at Compile-Time

▪ A compiler plug-in enables a programmer to find bugs or to verify their absence

▪ E.g. null pointer, immutability etc

Page 34: Java World, Java Trends, Java 8 and Beyond (iForum - 2014)

34

Checker Framework

import org.checkerframework.checker.nullness.qual.NonNull

public class CheckerProof {

public static void main(String[] args) {

String str=null;

proof(str);

}

public static void proof(@NonNull String s) {

System.out.println(s);

}

}

Checker.java:5: error: [argument.type.incompatible] incompatible types in argument. proof(str); ^ found : @FBCBottom @Nullable String required: @Initialized @NonNull String1 error

Page 35: Java World, Java Trends, Java 8 and Beyond (iForum - 2014)

35

Nashorn – JavaScript Engine for JVM

▪ You can implement you code in JavaScript

▪ Java seen from Nashorn

▪ Nashorn seen from Java

▪ Applications

▪ Client (Extending FX)

▪ Embedded/Mobile

▪ Server-side scripting

▪ Server-side JavaScript on the JVM with Nashorn (Avatar.js)

Page 36: Java World, Java Trends, Java 8 and Beyond (iForum - 2014)

36

jjs - command-line tool (JDK)

jjs Hello.js

Hello.js:

var hello = function () { print("Hello Nashorn"); } hello();

Page 37: Java World, Java Trends, Java 8 and Beyond (iForum - 2014)

37

Java seen from Nashorn

var HashMap = Java.type('java.util.HashMap');

var map = new HashMap();

map.put('foo', 'foo1');

map.put('bar', 'bar1');

for each(var e in map.keySet()) print(e);

for each(var e in map.values()) print(e);

Page 38: Java World, Java Trends, Java 8 and Beyond (iForum - 2014)

38

Nashorn seen from Java

import javax.script.*;

public class Nashorn {

public static void main (String[] args) {

ScriptEngineManager m = new ScriptEngineManager();

ScriptEngine e = m.getEngineByName("nashorn");

try {

e.eval("print('hello');"); }

catch (final ScriptException se) {

}

}

}

Page 39: Java World, Java Trends, Java 8 and Beyond (iForum - 2014)

39

Java 9 and Beyond

Page 40: Java World, Java Trends, Java 8 and Beyond (iForum - 2014)

40

Thank you for your attention!