Wednesday, 21 June 2017

Java 8 New Features : Lambda Expressions, Optional Class, Defender Methods With Examples

Java 8 is already released by Oracle last year. It is the most feature rich update then the previous version 6 and 7 , which both were somehow minor updates .

The major features of the java 8 are as follows :
  1. Introduction of Optional
  2. Defender Methods
  3. Lambda Expressions

1. Introduction Of Optional :


The main benefit of Optional is to Avoid null pointer exception : There is another class named Optional in the util package , as it is used to avoid the null pointer exception , If the value is present then it will return the true value otherwise it will show the false value  As it is boolean the value must be between false and true .


In java language , to access an object we use reference type .And when we are unable  to have a specific object to point to , then we set the value of the reference null , indicating it is not pointing to any object .
Null is a literal (also known as constant ) in java .

public static Cat  find(String name, List;Cat; cats) {
   for(Cat cat : cats) {
      if(cat.getName().equals(name)) {
         return cat;
      }
   }
   return null;
}

Method signature shows that  this method may not return a value but a null reference .

Cat;Cat; cats = asList(new Cat("lion"),
                            new Cat("snow leopard"),
                            new Cat("tiger"));

Cat found = find("mountain lion", cats);
//some code in between and much later on (or possibly somewhere else)...
String name = found.getName(); //uh oh!

2. Defender Methods :


Till java 7 , every interface only has method declaration and no implementations  , and any non abstract class which implements the  interface has to implement every method in interface

public interface MyInterface  {
   public void doAnyWork();
}

class MyInterfaceImpl  implements   MyInterface {
  @Override
 public  void doAnyWork(){
 System.out.println("Do Any work in the class " );
}

public static void main (String args[])
 MyInterfaceImpl  myobject= new MyInterfaceImpl();
 myobject.doAnyWork();
 }
}

Now suppose if i add some method doMoreWork()  abstract method in MyInterface  interface , then , if we try to compile the code , we will get compiler error

$javac .\MyInterface.java .\MyInterface.java:18: error: MyInterfaceImpl is not abstract and does not override abstract method doMoreWork() in MyInterface
class MyInterfaceImpl implements MyInterface{
^ 1 error

 And this limitation makes the situation almost impossible to change the interface or APIs . To overcome this challenge  , java 8 introduces default method also known as defender method .

public interface MyInterface  {
   public void doAnyWork();
// A default  method in the interface  created using "default"  keyword

   default public void doMoreWork(){
    System.out.println ("Do More Work implementation in the interface ") ; 
  }
}

class MyInterfaceImpl  implements   MyInterface {
  @Override
 public  void doAnyWork(){
 System.out.println("Do Any work implementation in the class " );
}
/*
 * Not required to override to provide an implementation
 * for doMoreWork
 */
public static void main (String args[])
 MyInterfaceImpl  myobject= new MyInterfaceImpl();
 myobject.doAnyWork();
 myobject.doMoreWork();
 }
}

And the output of the code will be : Do Any Work implementation in the class Do More Work implementation in the interface

3. Lambda Expressions :


Lambda expression is explained  in the pdf below: