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.
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.
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).
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 !
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.
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)
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.
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.
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
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.
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