What is lambda? and why do we need it?

When I started coding GUI application using Java, I realized my code was cluttered with too many anonymous classes. Each time, I had to handle an event the inlined implementation of ActionListener introduced nested and unreadable lines of code. How many of you have written code like below

signUpUserBtn.addActionListener(new ActionListener(){
  public void actionPerformed(ActionEvent e){
    someDelegate.handleUserSignUp(data);
  }
});

Continue reading

Java 8 – Method Reference – Is it Magic?

Amongst the notable features in Java8, Method Reference adds Shorthand syntax for a lambda expression. In most cases, Lambda expression does nothing more than just calling a single method. If you find yourself doing that most often, its time to look at Method reference. It can be used to replace entire Lambda expression, let’s have a look at following example
Continue reading

Java 8 – What is functional Interface?

Functional interface is a new addition to Java 8. In simple terms, an interface which has one single abstract method is qualified as Functional interface. The built-in classes in JDK are full of examples of Functional Interfaces(Runnable, Callable, ActionListener). Java 8 formalized such interface definition with @FunctionalInterface annotation. FunctionalInterface is also useful while creating lambda expressions. Those who are not familiar with Lambda expression, it was again a new feature introduced in Java 8. Lambda helps developers to write concise code. Its expressive syntax saves developer’s time. Before Lambda, for every single method declaration of an interface, developers had to create Class. Eventually, an application source code is filled with many small classes which just provide an implementation for one single method. FunctionalInterface and Lambda expressions are considered to be best practice for writing neat code. Lets have a look at how JavaDoc describes FunctionalInterface
Continue reading