Java 8 features

When we talk about Java 8 concepts like Functional Interfaces and Lambda expressions , Streams , Concurrency API improvements,Method references prevail the discussion. Let's try to explain them in a simple way. Will start with few basic concepts first and use them as base for understanding the Java 8 concepts.

Imperative Programming vs Functional Programming

Imperative programming is a programming style where developer writes program using statements. The developer concentrates on how to accomplish the functionality. The code is nothing but the alogithm used to accomplish the functionality. Let's do an example here to find all the students with grade 'A' and class 12. If we are not worried about the cost of the algorithm the code may look something like below.

for(int i=0; i< list.size(); i++)
{
 if("A".equals(list.get(i).getGrade()) && list.get(i).getClassNo() == 12){
  System.out.println(list.get(i).getSno());
 }
}

Functional programming on the other hand is a declarative programming style where the developer is more focused on what to do and not on how to do. We see usage of functions where emphasis is on what the function does or returns. Once we are good with lambda expression we can write the same above code as below.

list.stream()
 .filter(l -> "A".equals(list.get(i).getGrade()) && l.getClassNo() == 12 )
 .forEach( l -> System.out.println(l.getSno()));

Click here to read about Anonymous Classes or directly jump on to Functional Interfaces