Lambda expressions are a common feature in many languages, in particular those that follow the paradigm Functional Programming (the term itself comes from Calculus Lambda, mathematical foundation that supports this paradigm), but which have recently been introduced in languages of other paradigms (such as, in this case, the Object-Oriented/Imperative). To understand them, it is necessary to know the concepts of first class functions and literal.
Traditionally in Java, a method (function, procedure) exists only as a member of a class. This means that although you may have a variable "pointing" to an object, you cannot store a method in a variable. All that is allowed to reference in a language (in the case primitive objects or types), to pass as parameter to other functions, etc., is said to be "first class".
Other languages, however, allow functions and other things (such as classes) to be referenced and passed as arguments. Example using Javascript:
function teste() { alert("teste"); }
var x = teste;
x(); // Alerta "teste"
Support for first class functions greatly simplifies the construction of certain functions. For example, if we want to sort a list, and we would like to specify a specific comparison criterion, we pass a function as a parameter to the sort method:
ordena([...], function(a,b) { /* compara A e B */ });
Instead of having to create a specific class to contain such a function:
class MeuComparador implements Comparador {
int comparar(Object a, Object b) { /* compara A e B */ }
}
ordena([...], new MeuComparador());
Lambda expressions go a step further, not only allowing you to pass functions as parameters to other functions*, but also allowing them to be expressed as literals. A literal is a notation that represents a fixed value in the source code, that is, through the use of the syntax itself you can create an object that would otherwise require the combination of two or more different functionalities. Example (regular expressions in Javascript):
var regex = /.../; // Literal (o resultado já é um objeto RegExp)
var regex = new RegExp("..."); // Não literal (usa-se uma string, uma classe e o
// comando "new" para se construir o objeto
In Java 8, the literal for a lambda expression consists of a list of arguments (zero or more) followed by the operator ->
followed by an expression that must produce a value. Examples:
() -> 42 // Não recebe nada e sempre retorna "42"
x -> x*x // Recebe algo e retorna seu quadrado
(x,y) -> x + y // Recebe dois valores e retorna sua soma
To understand the internal details, how this impacts the rest of the program, how the discovery of types is made, etc, I suggest reading the official documentation (in English), because it is still a new feature (at least in this language). As for the advantages, the main one is the concision - do more by writing less code. There is often no reason to require a function to be always accompanied by a class, and the use of these expressions avoids many unnecessary constructs.
*Note: as pointed out by @dstori, the introduction of lambda expressions in Java did not make the functions first-class, since these expressions are converted into anonymous classes by the compiler (that is, it is not yet possible to make direct reference to a method in Java, only indirect through the object that defines it).
I arrived late, unfortunately already closed your question. My suggestion is simply that you take some cool project to do and start experimenting. You can learn fast and you will see that the code is much better with Amblas. He is something even Generics was 8 years ago: At first, everyone thought it was too complicated, too tricky and unnecessary, but over time the people began to realize that it was actually a very important thing that you couldn’t understand because it wasn’t in java from the beginning.
– Victor Stafusa
Placed a question in the goal concerning this question.
– utluiz
I voted to reopen, as for the adoption of lambda had to be justified by the JCP rules in JSR-335, and these justifications can be presented objectively as an answer here.
– Daniel C. Sobral