Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Java 8, The New Journey by Lin Dejie

Steve Teo
September 04, 2014

Java 8, The New Journey by Lin Dejie

DBS ITT Lunch and Learn Tech Sharing September 2014

Topic: Java 8, The New Journey
Speaker: Dejie Lin

Many popular programming languages nowadays have a language feature known as "lambdas" aka "closures". Not only the classic functional languages such as Lisp or Scheme, but also younger ones such as JavaScript, Python, Ruby, Groovy, Scala, C#, and even C++ has lambdas. Some of these lambda-supporting languages run on the Java Virtual Machine, and, naturally, Java as the most prominent programming language on the JVM did not want to be left behind. A discussion regarding the addition of lambdas/closures began in 2006.
▪ 2006 – Gosling: “We will never have lambda in Java”
▪ 2007 – 3 different proposal for lambda in java, but the conclusions weren’t converge..
▪ 2008 – Mark Reinhold: “We will never have lambdas in Java”
In 2009 the effort grounded to a halt and it ultimately looked like Java would never ever be extended by a lambda or closure feature. Consequently, Java's immanent decline as a vibrant programming language was predicted and the slogan of "Java is the new Cobol" was born.

Better late than never. Java 8 was released on Mar 2014, with full fledged features of Functional Programming, Lambda expression, Stream, Default Method, Parallel almost free, new Date/Time API, ForkJoinPool, StampedLock, more and more. And the new journey begins..

Steve Teo

September 04, 2014
Tweet

More Decks by Steve Teo

Other Decks in Technology

Transcript

  1. Java History Year Milestone Features Codename 1991 Oak 1992 Java

    Named 1996.01.23 Java 1.0 1997.02.19 Java 1.1 Inner Class, JavaBeans, JDBC, RMI 1998.12.08 Java 1.2 Collection Framework, JIT compiler, Swing, 2D class library, BLOB, CLOB 2000.05.08 Java 1.3 JNDI, Hotspot
  2. Java History Year Milestone Features Codename 2002.02.06 Java 1.4 XML

    parser (JAXP), JCE (Cryptography), assert keyword, Logging API, IPv6 Support, Image I/O 2004.09.30 Java 5.0 Generics, Enhanced For loop, static import, autoboxing, Enum, Annotation, Concurrency Package Annotation, Concurrency Package 2006.12.11 Java 6 Scripting language support, Improved web service, JDBC 4.0, JVM improvement, LDAP support
  3. Java History Year Milestone Features Codename 2011.07.28 Java 7 JVM

    supports dynamic languages 2011.07.28 Java 7 JVM supports dynamic languages (invokedynamic bytescode), String in switch statement,Type Inference, Multiple exception handling, Automatic resource Management (Try with resources), Java nio package, fork/join framework.. 2014.03.18 Java 8 Lambda expression, pipeline and stream, Default method, Date and Time API, Default method, Date and Time API, Nashorn (Javascript engine), Parallel operators, remove Permgen, hashmap.. 2016 Java 9 Project Jigsaw, sjavac, Multitenancy, support multi-gigabyte heap, self-tunning JVM
  4. Agenda ▪ Functional Programming ▪ Lambda ▪ Lambda ▪ Functional

    Interface, Default method ▪ Stream ▪ Metaspace
  5. Functional Programming vs Imperative Programming Imperative Method Function How vs

    What Focus on HOW Focus on What to be done, less HOW What Mutation Method mutates, modifies data, produce side effect. A PURE function does not mutate, it just inspects data, and produce results in form of new data. Code vs Object is the first class Function is the first class citizen. Code vs Data (Citizenship) Object is the first class citizen. Methods are code, they are invoked and executed. Function is the first class citizen. Functions are code-as-data; they are not only executable, but also passed around like data.
  6. What is Lambda Expression ▪ Lambda (short for Lambda Expression)

    ▪ Formerly known as Closure; Lisp ▪ Formerly known as Closure; Lisp ▪ Keep it simple: a lambda expression denotes a piece of functionality.
  7. Lambda Expression ▪ (int x) ->{return x+1;} ▪ (String s1,

    String s2) ->{return s2.length() – s1.length();} ▪ str -> System.out.println(str); ▪ (s1, s2)-> s2.length()-s1.length(); ▪ () -> System.out.println(this); Parameter Body ▪ () -> System.out.println(this); ▪ A method without name , behaves similar way as Anonymous Inner Class, Single Abstract Method
  8. Lambda Express is to replace AIC? ▪ AIC : can

    have more than one method ▪ AIC: scope (this) ▪ AIC: scope (this)
  9. Stream (parallel is almost free) ▪ Fluent Programming Style ▪

    Targeting the multicore CPUs, Writing multi-thread code is hard ▪ Targeting the multicore CPUs, Writing multi-thread code is hard ▪ Is view to a collection – Streams do not store their elements, view/adapter to a data source (collection, array) ▪ Support bulk operations (foreach, filter, map, reduce, etc) ▪ Support bulk operations (foreach, filter, map, reduce, etc) ▪ Perform internal iteration
  10. How to obtain a stream ▪ Java.util.Collection<T> – Stream<T> stream(),

    sequential functionality ▪ numList.stream(); ▪ numList.stream(); – Stream<T> parallelStream(), parallel functionality ▪ numList.parallelStream();
  11. Stream – Bulk data operation ▪ filter – Build a

    new sequence that is the result of a filter applied to each element in the original collection original collection ▪ Accounts.filter (a-> a.balance () > 1_000_000); ▪ map – Build a new sequence, where each element is the result of a mapping from an element of original sequence ▪ Accounts.map( a -> a.balance()); ▪ reduce ▪ reduce – produce a single result from elements of sequence ▪ Accounts.map(a-> a.balance ()).reduce(0, (b1,b2) ->b1+b2);
  12. Hotspot Young Generation Young Generation Eden Survivor Old Generation Tenured

    Method Area (Stack, thread, PC) X X X X From To X Metaspace Heap Non-Heap Perm Gen JVM
  13. PermGen space is completely removed ▪ -XX:PermSize ▪ -XX:MaxPermSize ▪

    -XX:MaxPermSize ▪ No more java.lang.OutOfMemeryError: PermGen space
  14. Perm Gen vs Metaspace ▪ Perm Gen: – Part of

    Heap – Always fixed maximum size (-XX:MaxPermSize=…) – Always fixed maximum size (-XX:MaxPermSize=…) – Increase if not enough ▪ Metaspace: – Out of Heap – Unbounded by default – Limit if necessary (-XX:MaxMetaspaceSize=…)