java 8 new features
DESCRIPTION
Java 8 presentation for FinJUG. Covers new features like lambda, default methods and new date time.TRANSCRIPT
JAVA 818.3.2014
FinJUG - Petteri Hietavirta
@FinJUG @pethie
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!
Why such a long wait
• Security improvements
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
Gc / runtime
• Remove the Permanent Generation
• Retire Some Rarely-Used GC Combinations
• Enhanced Verification Errors
• Reduce Class Metadata Footprint
• Small VM
• Fence Intrinsics
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
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
i18n
• Improve Locale Data Packaging and
Adopt Unicode CLDR Data
• BCP 47 Locale Matching
• Unicode 6.2
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
Highlights
• Default methods
• Lambda, Stream API
• DateTime
Default methods
public interface Demo {
public void foo();
}
class DemoImpl implements Demo {
public void foo() {
…
}
}
public interface Demo {
public void foo();
public void bar();
}
class DemoImpl implements Demo {
public void foo() {
…
}
public void bar() {
….
}
}
public interface Demo {
public void foo();
default public void bar() {
…
}
}
class DemoImpl implements Demo {
public void foo() {
…
}
}
λ 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
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
Runnable
new Thread(new Runnable() {
@Override public void run({
System.out.println(”verbose");
}
}).start();
new Thread( () ->
System.out.println(”simple") ).start();
• 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));
Foreach
List names =
Arrays.asList("Java", "Joe",
"Sam");
names.forEach(n ->
System.out.println(n));
java.util.functional
public interface Predicate<T>{
boolean test(T t);
}
Predicate<String> startsWithJ = (n) ->
n.startsWith("J");
Stream API - filter
names.stream()
.filter(startsWithJ)
.forEach((n) ->
System.out.println(n))
map
names.stream()
.map((name) -> name.toUpperCase())
.collect(Collectors.toList())
reduce
int sum = 0;
list.forEach(e -> {
sum += e.size();
});
int sum = Stream.of(1,2,3,4)
.reduce(0,(a, b) -> a + b);
parallel
names.parallelStream()
.filter(…
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
JSR-310 Date Time API
• Build on experiences from Jodatime
• java.time
• Based on nanoseconds
• Clear, fluent, immutable, extensible
Immutable
LocalDate dateOfBirth =
LocalDate.of(2012, Month.MAY, 14);
LocalDate firstBirthday =
dateOfBirth.plusYears(1);
Adjusters
• Previous Thursday of any date
date.with(TemporalAdjuster.previous
(DayOfWeek.THURSDAY)));
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());
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);
}
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)");
Compact profiles
10Mb17Mb24Mb140Mb
Missing from Java 8
• Jigsaw
• Java 9 coming – maybe 2016?
IDE support
• IntelliJ IDEA 12 & 13
• Eclipse Kepler (4.3) & Luna (4.4)
• Netbeans 7.4+
Oracle launch webcastTuesday, 25 Mar 2014
https://blogs.oracle.com/java/entry/
plans_for_java_8
http://eventreg.oracle.com/profile/web/
index.cfm?PKWebId=
0x637279c68
Stream API presentations
• http://www.jfokus.se/jfokus14/preso/L
ambdas-Programming-
Lab.pdf
• http
://www.jfokus.se/jfokus14/preso/Navi
gating-the-stream-
API.pdf
Thank you!