Eclipse and Java 8 - Eclipse Day India 2013

  • 770 views
Uploaded on

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

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

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

Views

Total Views
770
On Slideshare
0
From Embeds
0
Number of Embeds
4

Actions

Shares
Downloads
40
Comments
0
Likes
0

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. © 2013 IBM Corporation | Eclipse and Java 8 1 ECLIPSE AND JAVATM 8 Noopur Gupta Eclipse JDT/UI Committer IBM Software lab noopur_gupta@in.ibm.com
  • 2. © 2013 IBM Corporation | Eclipse and Java 8 2
  • 3. © 2013 IBM Corporation | Eclipse and Java 8 3
  • 4. © 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.
  • 5. © 2013 IBM Corporation | Eclipse and Java 8 5 Project Lambda Functional Interfaces
  • 6. © 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
  • 7. © 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.
  • 8. © 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 !
  • 9. © 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).
  • 10. © 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 !
  • 11. © 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.
  • 12. © 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)
  • 13. © 2013 IBM Corporation | Eclipse and Java 8 13 Project Lambda Method References Example:
  • 14. © 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.
  • 15. © 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.
  • 16. © 2013 IBM Corporation | Eclipse and Java 8 16
  • 17. © 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
  • 18. © 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.
  • 19. © 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:
  • 20. © 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
  • 21. © 2013 IBM Corporation | Eclipse and Java 8 21