Java 8 new features : Lambda expressions , optional class , Defender methods with examples

In JavaOne 2013 , Oracle  reveals the details about the Java 8 .  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 :

 Introduction of Optional
 Defender Methods
 Lambda Expressions


Read Also :       Java 7 Features Revisited

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!



The question we face is : Will optional helps  us to get rid of null pointer exceptions or references . then it is sadly NO !!


You can find the detail of the Optional Class here :

https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html



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 :


Click here to Download pdf of Lamda expression
I would love to learn more java 8 features .Please mention in the comments about java 8 features or any other information you have  .

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry