What is Lamda Expression.
It is essentially, an anonymous (that is unnamed) method. However this method is not executed on its own. Instead it is used to implement a method defined by a functional interface. Thus a lamda expression results in a form of anonymous class . These are also referred to as closures.
If we are learning about the Lamda expressions, it is very much required to learn about the Functional Interfaces.
A Functional Interface is an interface that contains one and only one abstract method. It can have any number of default ,static methods.
Default Method.
It is possible to specify default behaviour for a method declared in an interface this is called a default method.
Before Java 8 when a new method is added to the interface then it has to be implemented in the Implementing classes but with the help of default method we can have methods with implementation without affecting the classes that implement the Interface.
Eg:Sample Program Here the Method greetAll is the default method and not required to be implemented in all
the implementing classes.
package com.searchendeca.java8Tutorials;
@FunctionalInterface
public interface GettingStarted {
abstract String greetMessage();
default void greetAll() {
System.out.println("Hello All");
}
}
class gettingMain{
public static void main(String args[]) {
GettingStarted getValue;
getValue = ()->"Welcome Syed Ghouse";
System.out.println(getValue.greetMessage());
}
}
Method Reference
A method refrence provides a way to refer to a method without executing it.
There are three ways of method refrence avalible .
1. Method Reference to static methods.
It can be called using. ClassName::MethodName
2.Reference to an instance method.
ContainingObject:: Instance Methods.
3. Refrence to Constructor.
className::new
Here is the sample program.
package com.searchendeca.java8Tutorials;
@FunctionalInterface
public interface MethodRefrence {
abstract void greetMessage();
default void greetAll() {
System.out.println("Hello All");
}
}
package com.searchendeca.java8Tutorials;
public class SampleMethodRefrence implements MethodRefrence {
@Override
public void greetMessage() {
System.out.println("Welcome Syed Ghouse Habib");
}
public static String greetAllMessage() {
// TODO Auto-generated method stub
return "Welcome Syed Ghouse Habib";
}
}
package com.searchendeca.java8Tutorials;
public class InvokerMethodRefrence {
public static void main(String args[]) {
System.out.println("**********************");
SampleMethodRefrence samRef = new SampleMethodRefrence();
MethodRefrence sam=SampleMethodRefrence::greetAllMessage;// Method refrence to static methods
MethodRefrence sam1=samRef::greetMessage;//Method Refrence to Non Static methods(instance method names)
sam1.greetMessage();
}
}
package com.searchendeca.java8Tutorials;
interface MessagePrint{
conctructMess getMessage(String msg);
}
class conctructMess{
conctructMess(String msg){
System.out.print(msg);
}
}
public class ConstructorReference {
public static void main(String[] args) {
MessagePrint hello = conctructMess::new; //Refrence to Constructor.
hello.getMessage("Hello");
}
}
No comments:
Write comments