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

Eclipse and Java 8 - Eclipse Day India 2013

Noopur
September 27, 2013

Eclipse and Java 8 - Eclipse Day India 2013

Talk on "Eclipse and Java 8" at Eclipse Day India (Bangalore) 2013.

Noopur

September 27, 2013
Tweet

More Decks by Noopur

Other Decks in Programming

Transcript

  1. © 2013 IBM Corporation | Eclipse and Java 8 1

    ECLIPSE AND JAVATM 8 Noopur Gupta Eclipse JDT/UI Committer IBM Software lab [email protected]
  2. © 2013 IBM Corporation | Eclipse and Java 8 4

    Project Lambda Functional Interfaces Old content, new name! Interfaces that define a single abstract method (excluding Object methods), are now called as Functional Interfaces. Examples: Common callback interfaces like Runnable and Comparator. Optionally capture the design intent with the @FunctionalInterface annotation.
  3. © 2013 IBM Corporation | Eclipse and Java 8 5

    Project Lambda Functional Interfaces
  4. © 2013 IBM Corporation | Eclipse and Java 8 6

    Problem: “Tiny” anonymous classes. Used in callbacks, runnables, event handlers, comparators etc. All we need! Project Lambda Lambda Expressions
  5. © 2013 IBM Corporation | Eclipse and Java 8 7

    Solution: Lambda Expression! Project Lambda Lambda Expressions A lambda expression is used to implement a functional interface, without creating a class or an anonymous class. Reduced runtime overhead compared to anonymous classes! VM and JRE optimizations in lambda meta factory. Method invocation with invokedynamic bytecode instruction.
  6. © 2013 IBM Corporation | Eclipse and Java 8 8

    Project Lambda Lambda Expressions Syntax: (formal parameter list) -> { expression or statements } Syntax variants: Concise syntax compared to anonymous classes !
  7. © 2013 IBM Corporation | Eclipse and Java 8 9

    Project Lambda Lambda Expressions Target type of a lambda expression must be a functional interface. Target type is inferred from the surrounding context. Lambda parameters' types can be inferred. A lambda is a method without a name and without an identity (no 'this'). Lambda Expressions do not define a new level of scope (just like for loops and catch clauses).
  8. © 2013 IBM Corporation | Eclipse and Java 8 10

    Project Lambda Lambda Expressions Lambda Expressions can capture (reference) local variables of enclosing contexts if the local variables are final or effectively final (if its initial value is never changed). Beneficial for memory management: Inner class instances always hold a strong reference to their enclosing instance can lead to memory leaks. Lambda expressions that do not capture members from the enclosing instance do not hold a reference to it !
  9. © 2013 IBM Corporation | Eclipse and Java 8 11

    Project Lambda Method References Problem: A lambda expression that just invokes a named method – has to provide a method body. Solution: A shorthand for lambda – method reference of the existing named method.
  10. © 2013 IBM Corporation | Eclipse and Java 8 12

    A method reference is used to refer to a method without invoking it. A constructor reference is used to refer to a constructor without creating a new instance of the named class or array type. Project Lambda Method References Kinds of method references:  A static method (ClassName::methName)  An instance method of a particular object (instanceRef::methName)  A super method of a particular object (super::methName)  An instance method of an arbitrary object of a particular type (ClassName::methName) Kinds of Constructor references:  A class constructor reference (ClassName::new)  An array constructor reference (TypeName[]::new)
  11. © 2013 IBM Corporation | Eclipse and Java 8 13

    Project Lambda Method References Example:
  12. © 2013 IBM Corporation | Eclipse and Java 8 14

    Project Lambda : Interface Improvements Default Methods Problem: Evolve interfaces without introducing incompatibility with existing implementations.  Standard Java libraries need new lambda-friendly methods. Solution: Default methods – means of painless API evolution.
  13. © 2013 IBM Corporation | Eclipse and Java 8 15

    Project Lambda : Interface Improvements Static Methods Problem: A common scenario – Java libraries having companion utility classes with static methods. Solution: Static methods – allow helper methods specific to an interface to live with the interface.
  14. © 2013 IBM Corporation | Eclipse and Java 8 17

    Type Annotations Before Java 8 : Annotations could only be applied to declarations. As of Java 8 : Annotations can also be applied anywhere you use a type! Support improved analysis of Java programs. Enabler for checker frameworks and static analysis tools such as Sonar and FindBugs. Declaration Annotation Type Use Annotation
  15. © 2013 IBM Corporation | Eclipse and Java 8 18

    Type Annotations @ Examples: To declare a non-empty array of English-language strings: Each non-static method has an implicit formal parameter, this, which is called the receiver. In Java 8, it is permitted to explicitly declare the method receiver as the first formal parameter. The only purpose of writing the receiver explicitly is to make it possible to annotate the receiver’s type.
  16. © 2013 IBM Corporation | Eclipse and Java 8 19

    Type Annotations Use of a type name as a scoping mechanism is not a type use, hence cannot be annotated. Examples:
  17. © 2013 IBM Corporation | Eclipse and Java 8 20

    Set up Eclipse IDE (unreleased beta impl): http://wiki.eclipse.org/JDT_Core/Java8#What_to_do_to_set _up_the_IDE Java 8 features: http://openjdk.java.net/projects/jdk8/features Lambda FAQ: http://www.lambdafaq.org Trying Out Lambda Expressions in the Eclipse IDE: http://www.oracle.com/technetwork/articles/java/lambda- 1984522.html Type Annotations Specification: http://types.cs.washington.edu/jsr308/specification/java- annotation-design.html