• Save
Project Lambda in Java SE 8
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share

Project Lambda in Java SE 8

  • 978 views
Uploaded on

Video and slides synchronized, mp3 and slide download available at http://bit.ly/ZlOmQ9. ...

Video and slides synchronized, mp3 and slide download available at http://bit.ly/ZlOmQ9.

Daniel Smith details some of the new features prepared for Java 8 by Project Lambda: lambda expressions, default methods, and parallel collections. Filmed at qconsf.com.

Daniel Smith is a Java language designer and compiler developer at Oracle. He currently leads the specification efforts for JSR 335: Lambda Expressions for the Java Programming Language. He received a PhD in Computer Science from Rice University, doing research on type inference in object-oriented languages. He previously worked at LogicBlox.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
978
On Slideshare
978
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
0
Comments
0
Likes
3

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 131Project Lambda in Java SE 8Daniel SmithJava Language DesignerThursday, November 8, 12
  • 2. InfoQ.com: News & Community Site• 750,000 unique visitors/month• Published in 4 languages (English, Chinese, Japanese and BrazilianPortuguese)• Post content from our QCon conferences• News 15-20 / week• Articles 3-4 / week• Presentations (videos) 12-15 / week• Interviews 2-3 / week• Books 1 / monthWatch the video with slidesynchronization on InfoQ.com!http://www.infoq.com/presentations/Project-Lambda-Java-SE-8-SF
  • 3. Presented at QCon San Franciscowww.qconsf.comPurpose of QCon- to empower software development by facilitating the spread ofknowledge and innovationStrategy- practitioner-driven conference designed for YOU: influencers ofchange and innovation in your teams- speakers and topics driving the evolution and innovation- connecting and catalyzing the influencers and innovatorsHighlights- attended by more than 12,000 delegates since 2007- held in 9 cities worldwide
  • 4. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 132The following is intended to outline our general product direction. It is intendedfor information purposes only, and may not be incorporated into any contract.It is not a commitment to deliver any material, code, or functionality, and shouldnot be relied upon in making purchasing decisions. The development, release,and timing of any features or functionality described for Oracle’s productsremains at the sole discretion of Oracle.Thursday, November 8, 12
  • 5. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13The Java Programming Language•Around 9,000,000 developers worldwide•17 years old•4 major revisions (1996, 1997, 2004, 2013...)•[Insert staggering number] of companies very heavily invested•Formally standardized and evolved via community3Thursday, November 8, 12
  • 6. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13Evolving a Major Language•Adapting to change•Righting what’s wrong•Maintaining compatibility•Preserving the core4Thursday, November 8, 12
  • 7. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 135Project Lambda:Function Values in JavaThursday, November 8, 12
  • 8. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13Object subclass: Widget [draw: canvas [ ... ]click [ ... ]]gui add:(Widget new).(define f(lambda (x) (* x x)))(map nums f)Code as Data6Thursday, November 8, 12
  • 9. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13interface Runnable {void run();}Thread hello = new Thread(new Runnable() {public void run() {System.out.println(“Hello, world!”);}});Status Quo in Java 1.17Thursday, November 8, 12
  • 10. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13interface Predicate<T> {boolean accept(T arg);}lines.removeAll(new Predicate<String>() {public boolean accept(String line) {return line.startsWith(“#”);}});Status Quo in Java 58Thursday, November 8, 12
  • 11. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13interface Predicate<T> {boolean accept(T arg);}lines.removeAll(line -> line.startsWith(“#”));What We Wish It Looked Like9Thursday, November 8, 12
  • 12. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13Why Functions in Java? Adapting to Change10•Widely-adopted programming style• 1995: functions-as-values is too hard to understand• Now: almost everybody has them (even C++)•Physical constraints cause changing models• 1995: sequential execution, mutation• Today: concurrency, immutability•A gentle push in the right directionThursday, November 8, 12
  • 13. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13Why Functions in Java? Better Libraries11•Lots of applications...•Our priorities:• Collections• Concurrencypublic class ForkBlur extends RecursiveAction {private int[] mSource;private int mStart;private int mLength;private int[] mDestination;public ForkBlur(int[] src, int start, int length, int[] dst) {mSource = src;mStart = start;mLength = length;mDestination = dst;}// Average pixels from source, write results into destination.protected void computeDirectly() {for (int index = mStart; index < mStart + mLength; index++) {mDestination[index] = blur(index, mSource);}}protected static int sThreshold = 10000;protected void compute() {if (mLength < sThreshold) {computeDirectly();return;}int split = mLength / 2;invokeAll(new ForkBlur(mSource, mStart, split, mDestination),new ForkBlur(mSource, mStart + split, mLength - split, mDestination));}}Thursday, November 8, 12
  • 14. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13Brief History•1997: Odersky/Wadler experimental “Pizza” work•1997: Java 1.1 with anonymous classes•2006-2008: Vigorous community debate•2009: OpenJDK Project Lambda formed•2010: JSR 335 filed12Thursday, November 8, 12
  • 15. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 1313Java 8 Language Concepts & Features Lambda expressions Functional interfaces Target typing Method references Default methodsThursday, November 8, 12
  • 16. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13Lambda Expressions14Thursday, November 8, 12
  • 17. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13widget -> {if (flag) widget.poke();else widget.prod();}(int x, int y) -> {assert x < y;return x*y;}x -> x+1(s,i) -> s.substring(0,i)(Integer i) -> list.add(i)() -> System.out.print(“x”)cond -> cond ? 23 : 57Lambda Expressions15Thursday, November 8, 12
  • 18. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13void cut(List<String> l,int len) {l.updateAll(s ->s.substring(0, len));}Variable Capture16•Lambdas can refer to variablesdeclared outside the body•These variables can be final or“effectively final”• Works for anonymous classes,tooThursday, November 8, 12
  • 19. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13Meaning of Names in Lambdas17•Anonymous classes introduce a new “level” of scope• ‘this’ means the inner class instance• ‘ClassName.this’ is used to get to the enclosing class instance• Inherited names can shadow outer-scope names•Lambdas reside in the same “level” as the enclosing context• this refers to the enclosing class• No new names are inherited• Like local variables, parameter names can’t shadow other localsThursday, November 8, 12
  • 20. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13Functional Interfaces18Thursday, November 8, 12
  • 21. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13String -> int(String, int, boolean) -> List<? extends Integer>(String, Number) -> Class<?> throws IOExceptionFunction Types in Java?19Thursday, November 8, 12
  • 22. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13Function Types in Java: Functional Interfaces20Thursday, November 8, 12
  • 23. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13Common Existing Functional Interfaces•java.lang.Runnable•java.util.concurrent.Callable<V>•java.security.PrivilegedAction<T>•java.util.Comparator<T>•java.io.FileFilter•java.nio.file.PathMatcher•java.lang.reflect.InvocationHandler•java.beans.PropertyChangeListener•java.awt.event.ActionListener•javax.swing.event.ChangeListener21Thursday, November 8, 12
  • 24. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13Attributes of Functional Interfaces•Parameter types•Return type•Method type arguments•Thrown exceptions•An expressive, reifiable type name (possibly generic)•An informal contract22Thursday, November 8, 12
  • 25. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13Shiny New Functional Interfaces*•java.util.functions.Predicate<T>•java.util.functions.Factory<T>•java.util.functions.Block<T>•java.util.functions.Mapper<T, R>•java.util.functions.BinaryOperator<T>23* Names and concepts in libraries are still tentativeThursday, November 8, 12
  • 26. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13/** Creates an empty set. */public interface SetFactory {<T> Set<T> create();}/** Performs a blocking, interruptible action. */public interface BlockingTask<T> {<T> T run() throws InterruptedException;}Declare Your Own24Thursday, November 8, 12
  • 27. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13Target Typing25Thursday, November 8, 12
  • 28. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13// Runnable: void run()Runnable r =() -> System.out.println(“hi”);// Predicate<String>: boolean test(String arg)Predicate<String> pred =s -> s.length() < 100;Assigning a Lambda to a Variable26Thursday, November 8, 12
  • 29. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13Object o =() -> System.out.println(“hi”);// Predicate<String>: boolean test(String arg)Predicate<String> pred =() -> System.out.println(“hi”);Target Typing Errors27Thursday, November 8, 12
  • 30. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13long[][] arr ={ { 1, 2, 3 }, { 4, 5, 6 } };List<? extends Number> nums =Collections.emptyList();Set<Map<String, Object>> maps =new HashSet<>();Target Typing in Java 728Thursday, November 8, 12
  • 31. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13class Thread {public Thread(Runnable r) { ... }}// Runnable: void run()new Thread(() -> System.out.println(“hi”));Target Typing for Invocations29Thursday, November 8, 12
  • 32. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13interface Stream<T> {Stream<T> filter(Predicate<T> pred);}Stream<String> strings = ...;// Predicate<T>: boolean test(T arg)strings.filter(s -> s.length() < 100);Target Typing for Invocations30Thursday, November 8, 12
  • 33. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13<T> int m(Predicate<T> p);int m(FileFilter f);<S,T> int m(Mapper<S,T> m);m(x -> x == null);A Recipe for Disaster(Or: A Recipe for Awesome)31•Target typing•Overload resolution•Type argument inferenceThursday, November 8, 12
  • 34. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13Object o =(Runnable) () -> System.out.println(“hi”);Runnable r =condition() ? null : () -> System.gc();Mapper<String, Runnable> m =s -> () -> System.out.println(s);Other Target Typing Contexts32Thursday, November 8, 12
  • 35. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13Method References33Thursday, November 8, 12
  • 36. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13(x, y, z) -> Arrays.asList(x, y, z)(str, i) -> str.substring(i)() -> Thread.currentThread().dumpStack()(s) -> new File(s)Boilerplate Lambdas34Thursday, November 8, 12
  • 37. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13(x, y, z) -> Arrays.asList(x, y, z)Arrays::asList(str, i) -> str.substring(i)String::substring() -> Thread.currentThread().dumpStack()Thread.currentThread()::dumpStack(s) -> new File(s)File::newMethod (and Constructor) References35Thursday, November 8, 12
  • 38. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13Resolving a Method Reference36•Target type provides argument types•Named method is searched for using those argument types• Searching for an instance method, the first parameter is the receiver•Return type must be compatible with target returnThursday, November 8, 12
  • 39. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13Mapper<Byte, Set<Byte>> m1 = Collections::singleton;// SetFactory: <T> Set<T> create()SetFactory f2 = Collections::emptySet;Mapper<Queue<Float>, Float> m2 = Queue::peek;Factory<Set<String>> f3 = HashSet::new;Method References & Generics37Thursday, November 8, 12
  • 40. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13Default Methods38Thursday, November 8, 12
  • 41. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13New abstract methods: Badinterface Widget {double weight();double volume();double density();}New concrete methods: Goodabstract class Widget {abstract double weight();abstract double volume();double density() {return weight()/volume();}}Evolving APIs39Thursday, November 8, 12
  • 42. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13class Widgets {static double density(Widget w) {return w.weight()/w.volume();}}Workaround: Garbage Classes40•Not really a class•Non-idiomatic invocation syntax•Non-virtualThursday, November 8, 12
  • 43. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13interface Widget {double weight();double volume();default double density() {return weight()/volume();}}Default Methods: Code in Interfaces41Thursday, November 8, 12
  • 44. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13Multiple Inheritance?42class C:concrete m()interface I:default m()class Dinterface I:default m()interface J:abstract m()interface Kinterface J:default m()interface Kclass Cinterface I:default m()Thursday, November 8, 12
  • 45. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13interface Enumeration<E> extends Iterator<E> {boolean hasMoreElements();E nextElement();default boolean hasNext() { return hasMoreElements(); }default E next() { return getNext(); }default void remove() { throw new UnsupportedOperationException(); }default void forEachParallel(Block<T> b) { ... }}Evolving the Java Standard API43Thursday, November 8, 12
  • 46. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13Summary44Thursday, November 8, 12
  • 47. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13Goals for Project Lambda45•Make dramatic & necessary enhancements to the programming model•Smooth some rough edges in the language•Preserve compatibility•Maintain the essence of the Java languageThursday, November 8, 12
  • 48. Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Insert Information Protection Policy Classification from Slide 13Learning More•OpenJDK: openjdk.java.net/projects/lambda•JSR 335: www.jcp.org/en/jsr/detail?id=335•Me: daniel.smith@oracle.com•Download it and try it out!46Thursday, November 8, 12