Eclipse and Java 8 - Eclipse Day India 2013

21
© 2013 IBM Corporation | Eclipse and Java 8 1 ECLIPSE AND J AVA TM 8 Noopur Gupta Eclipse JDT/UI Committer IBM Software lab [email protected]

description

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

Transcript of Eclipse and Java 8 - Eclipse Day India 2013

© 2013 IBM Corporation | Eclipse and Java 8

1

ECLIPSE AND JAVATM 8

Noopur GuptaEclipse JDT/UI Committer

IBM Software lab

[email protected]

© 2013 IBM Corporation | Eclipse and Java 8

2

© 2013 IBM Corporation | Eclipse and Java 8

3

© 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.

© 2013 IBM Corporation | Eclipse and Java 8

5

Project Lambda

Functional Interfaces

© 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

© 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.

© 2013 IBM Corporation | Eclipse and Java 8

8

Project LambdaLambda Expressions

Syntax: (formal parameter list) -> { expression or statements }

Syntax variants:

Concise syntax

compared to

anonymous classes !

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

© 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 !

© 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.

© 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)

© 2013 IBM Corporation | Eclipse and Java 8

13

Project Lambda

Method References

Example:

© 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.

© 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.

© 2013 IBM Corporation | Eclipse and Java 8

16

© 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

AnnotationType Use

Annotation

© 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.

© 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:

© 2013 IBM Corporation | Eclipse and Java 8

21