Tuesday, August 11, 2015

Lamda Expression in Java 8


Lambda Expressions

Why Lambda in Java

Since beginning, the Java language always remained Object first language. Everthing should come after an Object 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.

Functions 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

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!

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 about Functional interface here.

Lambda expressions can only appear in places where they will be assigned to a variable whose type is a functional interface.

Introduction to Lambda Expressions

As mentioned in the java docs 
One issue with anonymous classes is that if the implementation of your anonymous class is very simple, such as an interface that contains only one method, then the syntax of anonymous classes may seem unwieldy and unclear. In these cases, you're usually trying to pass functionality as an argument to another method, such as what action should be taken when someone clicks a button. Lambda expressions enable you to do this, to treat functionality as method argument, or code as data. 

What is a Lambda Expression ?

Lambda expressions are anonymous methods, aimed at mainly addressing the Anonymous classes's syntax by replacing the machinery of anonymous inner classes with a lighter-weight mechanism in applicable cases.

Lambda Expressions enable you to encapsulate a single unit behaviour and pass it to other code.

Lambda expressions are supported by the following features:

Method References:
Method references are compact, easy-to-read lambda expressions for methods that already have a name.

Default Methods:
Default methods enable you to add new functionality to the interfaces of your libraries and ensure binary compatibility with code written for older version of those interfaces. They are interface methods that have an implementation and default keyword at the beginning of method signature.

New and Enhanced APIs

In Java SE8, new classes have been introduced and existing classes have been enhanced to take advantage of lambda expressions and streams. We can find most of the new and enhances classes in following packages:

Java.util: An existing package in which we integrated Java Collection Framework with streams and provide general utility functionality used by streams.
 • Java.util.function: A new package that contains general purpose functional interfaces that provide target types for lambda expressions and method references.
 • Java.util.stream: A new package that contains the majority of interfaces and classes that provide functionality to streams and aggregate operations.

Modified Packages: All the Wrapper classes have been enhanced with methods that are suitable for target of method references. For example: Integer.sum





No comments:

Post a Comment

Java garbage collection

In this post , we ’ ll take a look at how garbage collection works , why it ’ s important in Java , and how it works in...