What's new with Java 8?

Hello! I hope you had a good day and an awesome week. 😊

Java is a widely-used programming language. Java is fast, secure and reliable. From Java 6 to Java 7, much didn’t change, but Java 8 is a massive step forward. So many things changed and has made easier in Java 8. There are so many changes to look at but in this post, I’ll go through some major changes.


‘default’ and ‘static’ methods in interfaces

In previous versions of Java, interface methods can’t have a method body or a method implementation. This has changed in Java 8. In Java 8, interface methods which are declared using ‘default’ and ‘static’ keywords can have a method implementation. This introduce multiple inheritance. At first, Java didn’t support multiple inheritance due to the ‘Diamond Problem’. Diamond Problem is an ambiguity which occurs when a class inherits from two classes which has the same method. In the below diagram, we can see that B and C have inherited from A and D has inherited from both B and C.



In such a situation, compiler gets confused as which method to use. So, to solve this, Java compiler is made to identify those situations at the compiler time and throw an error asking to implement that method in the inherited class. In the above example, it’s D.


Lambda Expressions

This is one of the most astonishing features in Java 8. A 'lambda' is a code block which can be referenced and passed to another piece of code for future execution one or more times. Anonymous functions in other languages are lambdas. Java 8 introduced 'lambda expressions' which offer a simple syntax to create and use lambdas. Lambda expressions support only functional interfaces. A functional interface is an interface with exactly one abstract method. Annotation @FunctionalInterface is used to identify a functional interface.

Syntax for lambda expression is,
(argument) -> (body)

Below shown how a function was written before lambda,


Let’s see how to write this in lambda.


Main benefit of lambda expressions is the reduced lines of code.


Stream API

Stream is a new abstract layer introduced in Java 8. It represents a sequence of objects from a source which supports aggregate operations. Streams has the ability to manipulate multi-core architectures using parallel streams without worrying about multi-threading. This new feature is available in java.util.stream package. Stream API offers easy filtering, counting, mapping of collections and different ways to get slices or subsets of information out of them.

Let’s see how stream API is used.
  •          Map: Mapping each element to its result

  •          Filter: Eliminating elements based on a criterion



There is so much more to Stream API and Java 8 which will be discussed in a later post cause that is more than enough for a day!


I wish you all an amazing week ahead! Adios 😊

Comments

Popular posts from this blog

Double Submit Cookies Pattern - Node.js

Setting up the Environment with Node.js

Synchronizer Token Pattern - Node.js