Java 8 - Lambdas

¡Supera tus tareas y exámenes ahora con Quizwiz!

Constructor references

= Method references, except the name of the method is NEW Button :: new // reference to a Bottom constructor int[] :: new // constructor reference

Lambda expression - def

Block of code to pass around to be executed later Deferred execution: - other examples: runnable, comparator, button. - until now, you had to construct an object of a class that has the method.

Method references

x -> System.out.println(x) = System.out :: println :: operator separates method name from object or class name Three principal classes: object :: instanceMethod class :: staticMethod class :: instanceMethod

Lambda expression - syntax

(parameters) -> {expression} parameter types can be omitted if they can be inferred You never specify the result type, is inferred from context

Default methods and inheritance

If the same method is defined as a default method in two interfaces or in a superclass: 1. Superclass win. If a superclass provides a concrete method, default methods with the same name and parameters are ignored. 2. Interfaces clash: you must resolve the conflict by overriding the method, even if one of them provides an abstract method with the same name.

Default methods

Interface methods with concrete implementations Can be safely added to existing interfaces A concrete class that implement the interface must provide an implementation of abstract methods but it can choose to keep the implementation of the default method or override it.

Variable scope

Lambda expression scope: block of code values of free variables are captured by the lambda expression. You can only reference variables whose value doesn't change. Mutating variables in a lambda expression are not thread safe. Body: Same scope that a nested block same rules for name conflicts and shadowing this keyword refers to the this parameter of the method that creates the lambda.

Functional interfaces

Lambda expression: functional interface Interface with a single abstract method tag @FunctionalInterface Check exceptions: - catch the exception in the body of the lambda expression. - assign the lambda to an interface whose single method can throw an exception


Conjuntos de estudio relacionados

¿Prefieres ir al cine o descargar una película?

View Set

Pennsylvania Life Insurance - PA Laws/Rules

View Set

Intensive vs. Extensive Agriculture

View Set

Study guide-Skin Integrity & Wound Care PrepU

View Set

FBLA Introduction to Business Communication (Use This)

View Set

(skills/fundamentals) Lippincott Ch 4: Health of the Individual, Family, and Community

View Set