4 Lambdas Flashcards
What is a lambda expression
Functional programming uses lambda expressions to write code. A lambda expression
is a block of code that gets passed around. You can think of a lambda expression as an anonymous method. It has parameters and a body just like full-fl edged methods do, but it doesn’t have a name like a real method.
What is functional programming?
In Java 8, the language added the ability to write code using another style. Functional programming
is a way of writing code more declaratively. You specify what you want to do rather than dealing with the state of objects. You focus more on expressions than loops.
Functional programming uses lambda expressions to write code.
What are the special rules for writing lambdas?
1-The parentheses can only be omitted if there is a single parameter and its type is not explicitly stated.
2-Braces can be omitted when we only have a single statement.
3-Return & semicolon can be omitted when braces are not used.
4- Return and semicolon ARE REQUIRED when two or more statements.
5- Use {} to create blocks of code elsewhere.
- Remember that the parentheses
are only optional when there is one parameter and it doesn’t have a type declared.
Shortcut: a -> a.canHop()
Full syntax: (Animal a) -> { return a.canHop(); }
What is a “marker” interface?
An interface with no methods.
What is a “functional” interface?
An interface with one, and only one, abstract method. Even if an interface contains multiple default/and/or static methods, still functional if it has only one abstract method.
What is a a predicate?
It is a functional interface - (single abstract method) -the abstract method is called Test -the abstract method Test returns a boolean boolean test (T t)
Which is the correct arrow for lambda?
- >
- ->
The single arrow