JAVA 8
18.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
–...
Why such a long wait
• Security improvements
New features
• Lambda Expressions &Virtual Extension Methods
• Autoconf-Based Build System
• Lambda-Form Representation fo...
Gc / runtime
• Remove the Permanent Generation
• Retire Some Rarely-Used GC Combinations
• EnhancedVerification Errors
• R...
core / lang
• Launch JavaFX Applications
• GeneralizedTarget-Type Inference
• Annotations on JavaTypes
• DocTree API
• Add...
libs
• Parallel Array Sorting
• Bulk Data Operations for Collections
• Enhance Core Libraries with Lambda
• Charset Implem...
i18n
• Improve Locale Data Packaging and Adopt
UnicodeCLDR Data
• BCP 47 Locale Matching
• Unicode 6.2
security
• HTTP URL Permissions
• MS-SFU Kerberos 5 Extensions
• TLS Server Name Indication (SNI) Extension
• AEAD CipherS...
Highlights
• Default methods
• Lambda, StreamAPI
• 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() {
…
}
p...
public interface Demo {
public void foo();
default public void bar() {
…
}
}
class DemoImpl implements Demo {
public void ...
λ Lambda
• Lambda expressions are computational units
that operate on some data (input) and
produce some data (output)
• C...
Syntax
Argument list Arrow token Body
(int x, int y) -> x+y
(int x, int y) -> x + y
() -> 42
(String s) -> { System.out.pr...
Runnable
new Thread(new Runnable() {
@Override public void run({
System.out.println(”verbose");
}
}).start();
new Thread( ...
• SingleAbstract Method / functional interface
interface IntegerMath{
int operation(int a,int b);
}
Usage:
IntegerMath add...
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.startsWi...
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...
JSR-310 DateTime API
• Build on experiences from Jodatime
• java.time
• Based on nanoseconds
• Clear, fluent, immutable, e...
Immutable
LocalDate dateOfBirth = LocalDate.of(2012,
Month.MAY, 14);
LocalDate firstBirthday =
dateOfBirth.plusYears(1);
Adjusters
• PreviousThursday 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 =
dat...
public Temporal adjustInto(Temporal input) {
LocalDate date = LocalDate.from(input);
int day;
if (date.getDayOfMonth() < 1...
Period
LocalDate today = LocalDate.now();
LocalDate birthday = LocalDate.of(1960, Month.JANUARY, 1);
Period p = Period.bet...
Compact profiles
10Mb
17Mb
24Mb
140Mb
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 webcast
Tuesday, 25 Mar 2014
https://blogs.oracle.com/java/entry/plans_for_j
ava_8
http://eventreg.oracle.co...
Stream API presentations
• http://www.jfokus.se/jfokus14/preso/Lambda
s-Programming-Lab.pdf
• http://www.jfokus.se/jfokus1...
Thank you!
Upcoming SlideShare
Loading in …5
×

Java 8 new features

1,249 views

Published on

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

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Java 8 new features

  1. 1. JAVA 8 18.3.2014 FinJUG - Petteri Hietavirta @FinJUG @pethie
  2. 2. 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!
  3. 3. Why such a long wait • Security improvements
  4. 4. 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
  5. 5. Gc / runtime • Remove the Permanent Generation • Retire Some Rarely-Used GC Combinations • EnhancedVerification Errors • Reduce Class Metadata Footprint • SmallVM • Fence Intrinsics
  6. 6. core / lang • Launch JavaFX Applications • GeneralizedTarget-Type Inference • Annotations on JavaTypes • DocTree API • Add Javadoc to javax.tools • Remove the Annotation-ProcessingTool (apt) • Access to Parameter Names at Runtime • RepeatingAnnotations • Enhance javac to Improve Build Speed • DocLint
  7. 7. 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 BalancedTrees
  8. 8. i18n • Improve Locale Data Packaging and Adopt UnicodeCLDR Data • BCP 47 Locale Matching • Unicode 6.2
  9. 9. 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-NumberGeneration • Enhance the Certificate Revocation-Checking API • NSA Suite B Cryptographic Algorithms • SHA-224 Message Digests • PKCS#11 Crypto Provider for 64-bitWindows • Limited doPrivileged • Overhaul JKS-JCEKS-PKCS12 Keystores • JAXP 1.5: Restrict Fetching of External Resources
  10. 10. Highlights • Default methods • Lambda, StreamAPI • DateTime
  11. 11. Default methods public interface Demo { public void foo(); } class DemoImpl implements Demo { public void foo() { … } }
  12. 12. public interface Demo { public void foo(); public void bar(); } class DemoImpl implements Demo { public void foo() { … } public void bar() { …. } }
  13. 13. public interface Demo { public void foo(); default public void bar() { … } } class DemoImpl implements Demo { public void foo() { … } }
  14. 14. λ 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
  15. 15. Syntax Argument 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
  16. 16. Runnable new Thread(new Runnable() { @Override public void run({ System.out.println(”verbose"); } }).start(); new Thread( () -> System.out.println(”simple") ).start();
  17. 17. • SingleAbstract Method / functional interface interface IntegerMath{ int operation(int a,int b); } Usage: IntegerMath addition = (a,b) -> a+b IntegerMath multi = (a,b) -> a*b; System.out.println("Add lambda :"+ addition.operation(10, 4)); System.out.println("Multiply lambda :"+ multi.operation(10, 4));
  18. 18. Foreach List names = Arrays.asList("Java", "Joe", "Sam"); names.forEach(n -> System.out.println(n));
  19. 19. java.util.functional public interface Predicate<T>{ boolean test(T t); } Predicate<String> startsWithJ = (n) -> n.startsWith("J");
  20. 20. Stream API - filter names.stream() .filter(startsWithJ) .forEach((n) -> System.out.println(n))
  21. 21. map names.stream() .map((name) -> name.toUpperCase()) .collect(Collectors.toList())
  22. 22. reduce int sum = 0; list.forEach(e -> { sum += e.size(); }); int sum = Stream.of(1,2,3,4) .reduce(0,(a, b) -> a + b);
  23. 23. parallel names.parallelStream() .filter(…
  24. 24. 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
  25. 25. JSR-310 DateTime API • Build on experiences from Jodatime • java.time • Based on nanoseconds • Clear, fluent, immutable, extensible
  26. 26. Immutable LocalDate dateOfBirth = LocalDate.of(2012, Month.MAY, 14); LocalDate firstBirthday = dateOfBirth.plusYears(1);
  27. 27. Adjusters • PreviousThursday of any date date.with(TemporalAdjuster.previous (DayOfWeek.THURSDAY)));
  28. 28. 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());
  29. 29. 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); }
  30. 30. 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)");
  31. 31. Compact profiles 10Mb 17Mb 24Mb 140Mb
  32. 32. Missing from Java 8 • Jigsaw • Java 9 coming – maybe 2016?
  33. 33. IDE support • IntelliJ IDEA 12 & 13 • Eclipse Kepler (4.3) & Luna (4.4) • Netbeans 7.4+
  34. 34. Oracle launch webcast Tuesday, 25 Mar 2014 https://blogs.oracle.com/java/entry/plans_for_j ava_8 http://eventreg.oracle.com/profile/web/index.cf m?PKWebId=0x637279c68
  35. 35. Stream API presentations • http://www.jfokus.se/jfokus14/preso/Lambda s-Programming-Lab.pdf • http://www.jfokus.se/jfokus14/preso/Navigati ng-the-stream-API.pdf
  36. 36. Thank you!

×