In this ever changing world of technology there are obvious questions on the survival of a technology in the coming years but as very well said by Mark Reinhold, “Java is still not dead—and people are starting to figure that out.”
Welcome to the introduction to Java8. In this tutorial, you’ll learn about the new features added to Java8 and we’ll cover the topics step by step in details in our future posts.
Since the release of Java5 in 2004, undoubtedly the release of Java8 has been the greatest thing in Java world. It has added a lot of new features in Java as a language, as well as the libraries, compiler, tools and the JVM too. In this tutorial, we are going to discuss some important features Java8 has introduced us to.
It is also important to note that there have been a few interface improvements too.
There are many new features added to Java8. Here is a list of them with each briefly described. All of the following topics would be dealt with in details, later on in the tutorial.
STATIC method for Interfaces:
Interfaces can now define the static methods. A very common experience in Java libraries is that for some interface Foo, there will be a utility class Foos with static methods working with Foo instances. Now that the static methods exist in an interface, in most cases utility class can go away with the public methods.
These methods are available only with the implementing interfaces and not the classes implementing them.
DEFAULT method for Interfaces:
Similar to static methods, interfaces can have “default methods” as well. The difference is that static methods can not be overridden but a default method can be overridden in an implementation. If you write a static method with the same signature in the child class and annotate it with @Override annotation the compiler will throw an error.
In the case of a diamond problem(i.e. a class implementing multiple interfaces having a method with the same signature), it is mandatory for the child class to provide an implementation for the method.
Lambdas are also known as closures. These are the biggest awaited change in the language within the whole Java 8 release. It lets us treat the functionality as a method argument, or treat a code as data. This is the concept every functional developer is very familiar with but some developers might take some time to adjust to it as it does a lot of work behind the scene but with time and practise, I am sure you all will also love it.
The Lambda expression consists of the following:
- It is a comma-separated list of formal parameters enclosed in parentheses.
- The arrow token ->
- A body, that consists of a single expression or sometimes a statement block.
Ex – (param1, param2) -> param1 + param2
Lambda expressions fit into Java’s type system. Each lambda corresponds to a certain type, specified by an interface. So, a functional interface contains exactly one abstract method declaration. Each lambda expression would be matched to the abstract method.
Since the default methods are not abstract you’re free to add as many default methods as well as static methods to the interface and it will still be a functional interface.
Example – Comparator interface
Method and Constructor References:
Java 8 enables you to pass references of methods or constructors via the
:: keyword. This would further be explained next tutorials.
For example, a comparator can be implemented as –
This will have 3 sub parts –
- Accessing local variables
- Accessing fields and static variables
- Accessing Default Interface Methods
Built-in Functional Interfaces:
The JDK 1.8 API contains many built-in functional interfaces, some of them are :
Streams are designed in such a way that they can be processed sequentially or parallelly. Operations of sequential streams are performed on single cpu while operations of parallel streams are performed on multiple cpu. It is designed to use the multicore cpu capability to the fullest with the minimum changes and efforts.
For example – if we have to “sum” a large list of integers then we can parallelize this stuff, multiple threads will work on a specific segment of the list and merge the result in the end. This can significantly improve the performance although it has some limitations.
- Other topics such as improvements in Collection API, Date API, Concurrency API, IO operations, Annotations, and others will also to be covered in the course.
This would be all for this tutorial. The next tutorial will cover all features described thoroughly one by one.
Do come back for more. Hope this helps and you like the tutorial. Do ask for any queries in the comment box and provide your valuable feedback.
Share and subscribe.
Keep Coding!! Happy Coding!!