15
Java 8 at a glance Hidetomo Moriomoto http://github.com/mocchidesu 2014/05/05

Java 8 briefing

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Java 8 briefing

Java 8 at a glance

Hidetomo Moriomotohttp://github.com/mocchidesu

2014/05/05

Page 2: Java 8 briefing

Table of Contents

* Brief history of Java* What's new in Java 8* Why Lambda?* Sample Program &&

Bench mark

James Gosling: Father of Java

Page 3: Java 8 briefing

History of Java

Date Version Highlights

1991 Oak by James Gosling@Sun Microsystems

Named after oak tree stood outside his office. For small PDA device. Slight flavor of C

1996 Jan 23 JDK 1.0 “Oak” sounds similar to “awk”?

1997 Java 1.1 JDBC, reflection, JIT compiler

1998 Java2 (J2SE) Collection framework, Swing(GUI) API

2000 Java 1.3 JNDI, RMI

2002 Feb. Java 1.4 Regex, JAXP

2004 Sept. Java 1.5 Generics, annotation, enum, concurrency, varargs

2006 Dec. Java 1.6 Performance tune, JDBC4

2011 July Java 1.7 JVM support dynamic language

Page 4: Java 8 briefing

Java 8 - 03/18/2014 – came out

FeaturesDateTime API (no more new Date(), Calendar.getInstance()!)Nashborn JavaScript engine ($JAVA_HOME/bin/jjs)Default method in Interface / Improved concurrent

package / OptionalLambda expressions

@FunctionalInterface annotationsMethod Referencejava.util.Stream package

Backward CompatibilityBinary compatibility && Behavioral compatibility - ExcellentSource compatibility - Good

Page 5: Java 8 briefing

What is Lambda? (JSR335)

Lambda is a syntax that produces instance of Functional Interface.

Page 6: Java 8 briefing

Then, what’s Functional Interface?

Functional Interface is: Interface that has exactly one abstract

method- Runnable ( run() )- Comparator (compare() )See

@FunctionalInterface (java.lang.annotation)List of interface under java.util.function packageYou can define your own interface, of course.

Page 7: Java 8 briefing

Why Lambdas?

Leap to functional language (Haskell, Erlang, JavaScript, Lisp, Scala, Ocaml, etc.)

Single-method interface (syntax sugar)

Provide a path to multicore (stream vs. parallel stream) -> see demo.

No more for loop (internal loop vs. external loop)

Page 8: Java 8 briefing

Before moving forward…What is Functional Language?

In short … function is the 1st level Object/** JavaScript can pass method as a parameter.*/var doSomething = function(name){

console.log(name + "is going to do something someday");}

function useSomething(func, param){func(param);

}

Event handler

Page 9: Java 8 briefing

Lambda Syntax – how it looks(parameter) -> { // do something} ; // that’s it!Further simplify with “Type interface” - best guess@FunctionalInterface private interface HelloWorld{ public String hello(String name); }

Old anonymous class HelloWorld hello = new HelloWorld() {

@Override public String hello(String name) { return "Hello, " + name; } };Ex 1) -> Get Simplified with Lambda (You don’t need to specify method name)

(String name) -> { return “Hello “+name + “!!”; };Ex 2) Remove String -> JVM best guess – type interface

(name) -> { return “Hello “+name+”!!”; };Ex 3) Remove return – type interface

(name) -> { “Hello “+name+”!!”; };Ex 4) Remove brackets for single implementation.

(name) -> “Hello “+name+”!!”;Ex 5) Remove () if parameter is one

name -> “Hello “+name+”!!”;(name -> “Hello “+name+”!!”;

Page 10: Java 8 briefing

Sample (Stream API)

Start with .stream(), chain with intermediate operators, end with terminal operator.

Intermediate Operator (filter, map, sorted, distinct, limit)

Terminal Operator (forEach, collect, reduce, iterator)

Page 11: Java 8 briefing

Stream cont.

listOfSomething.stream().filter( element -> element.age > 30) //.map( ) // not evaluated.sorted() // not evaluated.limit() // not evaluated…..collect( Collectors.toList() ); Finally

evaluate

Page 12: Java 8 briefing

Let’s Bench Mark

Sample Code (Run com.hidetomo.Java8StreamBench.main)https://github.com/mocchidesu/Java8sample.git

Input: 100,000 last name from HSADo:

Filter: Last name start with ‘m’Map: (Brace each name with [] bracket) Sort: Reverse orderDedupeLimit to top 2 results

Loop 1000 times.

Traditional Java7 For Loop approach vs. Sequential Lambda vs. Parallel Lambda. -> IntelliJ13

Page 13: Java 8 briefing

Conclusion (sorry, not good result)

Java 8 is backward compatible in binary level

Lambda is not only a syntax sugar It’s a new era of Java Performance gain? Will be improved.

Stay tuned. When use? NOW

Page 14: Java 8 briefing

Reference

Everything about Java 8 http://www.techempower.com/blog/2013/03/26/everything-about-java-8/

Oracle Compatibility Guide for JDK 8http://www.oracle.com/technetwork/java/javase/8-compatibility-guide-2156366.html

Lambda Peek under the Hood by Brian Goetz - Java One 2013http://www.slideshare.net/jaxlondon2012/lambda-a-peek-under-the-hood-brian-goetz10 Example of Lambda Expressions and Streams in Java 8http://javarevisited.blogspot.com/2014/02/10-example-of-lambda-expressions-in-java8.html

Page 15: Java 8 briefing

THANK YOU

Github.com/hidetomo/java8sample.git

Slideshare /hidetomomorimoto