Posts

Showing posts from August, 2018

Lambda Expression Basics and Syntax - Java8

Image
In simple term, a Lambda Expression is implementation of a Functional Interface. Lambda Expression is a way to provide implementation of a functional interface without creating a Class or Anonymous Inner Class. Note that Lambda can implement only a functional interface. Read more about Functional Interface Here is a functional interface named Processor which has single abstract method called process And let say there is processValue method, which takes a value and a Processor object as input, apply processor on the value and return the result. private int processValue(int value, Processor processor) { return processor.process(value); } Call processValue method using Anonymous Inner Inner Class as implementation of Processor interface. processValue(10, new Processor() { @Override public int process(int input) { return input * 2; } }); It’s easily noticeable the unnecessary verbose code that we had to write for anonymous inner class. Only important line which is d

Functional Interface - Java8

Image
Before Java 8, an interface could only declare one or more methods also known as Abstract Method (method with no implementation, just the signature). Starting with Java 8 an interface can also have implementation of one or more methods (knows as Interface Default Method) and static methods along with abstract methods. Interface Default Methods are marked default keyword. So the question is, what is Functional Interface? An interface with Single Abstract Method (SAM) is called Functional Interface. Which means -  An interface with Single Abstract Method is a Functional Interface An interface with Single Abstract Method and zero or more default methods and zero or more static method is also a valid Functional Interface. Below is a functional interface with Single Abstract Method Below is a functional interface with Single Abstract Method along with default method and static method. You must have noticed the @FunctionalInterface annotation. What does it mean?