TrabalhosGratuitos.com - Trabalhos, Monografias, Artigos, Exames, Resumos de livros, Dissertações
Pesquisar

Labdas Java

Monografias: Labdas Java. Pesquise 860.000+ trabalhos acadêmicos

Por:   •  31/7/2014  •  2.491 Palavras (10 Páginas)  •  185 Visualizações

Página 1 de 10

Java 8 Lambda Expressions Tutorial with Examples

By Viral Patel on January 29, 2014

Java is a first-class object-oriented language. With the exception of primitive data types, everything in Java is an object. Even an array is an Object. Every class creates instances that are objects. There is no way of defining just a function / method which stays in Java all by itself. There is no way of passing a method as argument or returning a method body for that instance.

Since the old days of Swing, we always had written anonymous classes if we wanted to pass some functionality to any method. For example the old event listener code used to look like:

someObject.addMouseListener(new MouseAdapter() {

public void mouseClicked(MouseEvent e) {

//Event listener implementation goes here...

}

});

Here we wanted to add some custom code to Mouse listener. We defined an anonymous inner class MouseAdapter and created its object. This way we passed some functionality to addMouseListener method.

In short, it is not easy to pass plain methods / functionalities in Java that can be passed as arguments. Due to this limitation Java 8 adds a brand new language level feature called Lambda Expressions.

Why Java needs Lambda Expressions?

Since its beginning, the Java language hasn’t evolved much if you ignore some of the features like Annotations, Generics etc. Mostly during its life Java always remained Object first language. After working with functional language like JavaScript, it becomes clear to one how Java enforce its strict object-oriented nature and strict typed on the source code. You see Functions are not important for Java. On their own they cannot live in Java world.

xkcd-functionalFunctions are first class citizens in a functional programming language. They exists on their own. You can assign them to a variable and pass them as arguments to other functions. JavaScript is one of the best example of an FP language. There are some good articles here and here that clearly describes the benefits of JavaScript as a functional language. A functional language provides very powerful feature called Closure that has quite a few advantages over traditional way of writing applications. A closure is a function or reference to a function together with a referencing environment — a table storing a reference to each of the non-local variables of that function. Closest thing that Java can provide to Closure is Lambda expressions. There is significant difference between a Closure and Lambda expression, but at least Lambda expression provides a good alternative to Closure.

In his quite sarcastic and funny blog post, Steve Yegge describes how Java world is strictly about Nouns. If you haven’t read his blog, go first read it. It’s funny, its interesting and it describe the exact reason why Java had to add Lambda expressions.

Lambda expression adds that missing link of functional programming to Java. Lambda expression let us have functions as first class citizen. Although this is not 100% correct, we will shortly see how Lambda expressions are not closures but they are as much close as we can get to closures. In languages that support first class functions, the type of the lambda expression would be a function; but in Java, the lambda expressions are represented as objects, and so they must be bound to a particular object type known as a functional interface. We will see in detail what Functional interface are.

Here is a nicely written article by Mario Fusco on Why we need Lambda Expression in Java. He explains why a modern programming language must have feature like closures.

Introduction to Lambda Expressions

lambda expressions in javaA lambda expression is an anonymous function (not 100% true for Java but lets assume it for time being). Simply put, it’s a method without a declaration, i.e., access modifier, return value declaration, and name.

It’s a shorthand that allows you to write a method in the same place you are going to use it. Especially useful in places where a method is being used only once, and the method definition is short. It saves you the effort of declaring and writing a separate method to the containing class.

Lambda expressions in Java is usual written using syntax (argument) -> (body). For example:

(arg1, arg2...) -> { body }

(type1 arg1, type2 arg2...) -> { body }

Following are some examples of Lambda expressions.

(int a, int b) -> { return a + b; }

() -> System.out.println("Hello World");

(String s) -> { System.out.println(s); }

() -> 42

() -> { return 3.1415 };

Structure of Lambda Expressions

Let’s check the structure of lambda expressions.

A lambda expression can have zero, one or more parameters.

The type of the parameters can be explicitly declared or it can be inferred from the context. e.g. (int a) is same as just (a)

Parameters are enclosed in parentheses and separated by commas. e.g. (a, b) or (int a, int b) or (String a, int b, float c)

Empty parentheses are used to represent an empty set of parameters. e.g. () -> 42

When there is a single parameter, if its type is inferred, it is not mandatory to use parentheses. e.g. a -> return a*a

The body of the lambda expressions can contain zero, one or more statements.

If body of lambda expression has single statement curly brackets are not mandatory and the return type of the anonymous function is the same as that of the body expression.

...

Baixar como (para membros premium)  txt (14.3 Kb)  
Continuar por mais 9 páginas »
Disponível apenas no TrabalhosGratuitos.com