Java World, Java Trends, Java 8 and Beyond (iForum - 2014)
-
Upload
olena-syrota -
Category
Technology
-
view
157 -
download
4
Transcript of Java World, Java Trends, Java 8 and Beyond (iForum - 2014)
JavaJava World, Java Trends, Java 8 and Beyond Olena Syrota
@osyrota
2
Agenda
▪ Java Today
▪ Java Trends
▪ Java 8
▪ Beyond Java 8
3
Java Today
4
Java Birthday
▪ Started in 1991
▪ Released in 1995
Who of you was born in 1991?
5
6
Java Today - Advantages
▪ Stable
▪ High performance
▪ Backward compatibility
▪ JVM
▪ Mature culture of build tools and practice
▪ JCP process and community involvement
▪ etc
7
Java Today - Disadvantage
▪ A lot of boilerplate code (imperative programming style)
▪ Every language has lambda
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
9
Polyglot Programming
10
Java 8 Released 18 March 2014
▪ 55 new features in Java SE 8
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)
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
13
Java 8
14
Java 8 Features to Discuss
▪ Lambda
▪ Bulk operations for collections
▪ Parallel operations on collections
▪ Date and Time API
▪ Annotations on Types
▪ Nashorn
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
16
Lambdas
( ) -> { }
17
Lambda Sample
interface Adder {
int add (int a, int b);
}
Adder adder = (a, b)->a+b;
18
Impact of Lambda
▪ Passing behavior as parameter has great impact on code
▪ Control is transferred from client to library
19
Internal Iteration
public void internalIteration(List<Person> voters) {
voters.forEach(v->System.out.println(v));
}
20
Parallel Internal Iteration
public void internalIteration(List<Person> voters) {
voters.parallelStream().forEach(v->System.out.println(v));
}
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());
}
22
More Bulk Operations
public static double averageAgeOfVoters(List<Person> voters) {
return voters
.stream()
.mapToInt(v->v.getAge())
.average()
.getAsDouble();
}
23
Sequential version
public static Set<Ballot> unspoiledBallots(Set<Ballot> ballots) {
return ballots
.stream()
.filter(b->!b.isSpoiled())
.collect(Collectors.toSet());
}
24
Parallel version
public static Set<Ballot> unspoiledBallots(Set<Ballot> ballots) {
return ballots
.parallelStream()
.filter(b->!b.isSpoiled())
.collect(Collectors.toSet());
}
25
Parallel Computations in Java:form Thread to Project Lambda
26
Interface Evolution
▪ Functional interfaces
▪ Default methods
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); } } }
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); }
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
30
Date and Time API
▪ Why new Date and Time API?
▪ Immutable-value classes ( immutable => thread-safe)
▪ Domain-driven API design
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);
}
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;
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
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
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)
36
jjs - command-line tool (JDK)
jjs Hello.js
Hello.js:
var hello = function () { print("Hello Nashorn"); } hello();
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);
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) {
}
}
}
39
Java 9 and Beyond
40
Thank you for your attention!