# Lambdas & Method References — Advanced Java

Source: https://www.geekswithgeeks.com/en/advanced-java/adv-lambdas

> Pass behaviour as data with lambdas over functional interfaces like Function and Predicate, use method references, and respect effectively-final capture.

## Functional interfaces

A lambda is an instance of an interface with exactly one abstract method (`Runnable`, `Comparator`, `Function`, `Predicate`, `Supplier`, `Consumer`). The compiler infers parameter types from the target type.

## Syntax forms

A method reference (`String::toUpperCase`, `System.out::println`, `ArrayList::new`) is a shorthand for a lambda that just forwards its arguments.

```java
Comparator<String> byLen = (a, b) -> a.length() - b.length();
Runnable task = () -> System.out.println("run");
Function<String, Integer> len = String::length;
list.sort(Comparator.comparing(String::length));
```

## Effectively final capture

A lambda can read local variables from the enclosing scope only if they are `final` or never reassigned. Instance and static fields have no such limit.
