Java 8 Features – The ULTIMATE Guide--reference
Now,it is time to gather all the major Java 8 features under one reference post for your reading pleasure. Enjoy! Table Of Contents
1. IntroductionWith no doubts,??is the greatest thing in the Java world since Java 5 (released quite a while ago,back in 2004). It brings tons of new features to the Java as a language,its compiler,libraries,tools and the JVM (Java virtual machine) itself. In this tutorial we are going to take a look on all these changes and demonstrate the different usage scenarios on real examples. The tutorial consists of several parts where each one touches the specific side of the platform:
2. New Features in Java languageJava 8 is by any means a major release. One might say it took so long to finalize in order to implement the features every Java developer was looking for. In this section we are going to cover most of them. 2.1. Lambdas and Functional InterfacesLambdas (also known as closures) are the biggest and most awaited language change in the whole Java 8 release. They allow us to treat functionality as a method argument (passing functions around),or treat a code as data: the concepts every??is very familiar with. Many languages on JVM platform (Groovy,?,…) have had lambdas since day one,but Java developers had no choice but hammer the lambdas with boilerplate anonymous classes. Lambdas design discussions have taken a lot of time and community efforts. But finally,the trade-offs have been found,leading to new concise and compact language constructs. In its simplest form,a lambda could be represented as a comma-separated list of parameters,the?–>?symbol and the body. For example:
|
Please notice the type of argument?e?is being inferred by the compiler. Alternatively,you may explicitly provide the type of the parameter,wrapping the definition in brackets. For example:
? System.out.println( e ) );
In case lambda’s body is more complex,it may be wrapped into square brackets,as the usual function definition in Java. For example:
1? {
2
3
4
Lambdas may reference the class members and local variables (implicitly making them effectively?final?if they are not). For example,those two snippets are equivalent:
1
2?
3 System.out.print( e + separator ) );
And:
1??
3 System.out.print( e + separator ) );
Lambdas may return a value. The type of the return value will be inferred by compiler. The?return?statement is not required if the lambda body is just a one-liner. The two code snippets below are equivalent:
1? e1.compareTo( e2 ) );
And:
1 {
2?
3?
4
Language designers put a lot of thought on how to make already existing functionality lambda-friendly. As a result,the concept of?has emerged. The function interface is an interface with just one single method. As such,it may be implicitly converted to a lambda expression. The?java.lang.Runnable?and?java.util.concurrent.Callable?are two great examples of functional interfaces. In practice,the functional interfaces are fragile: if someone adds just one another method to the interface definition,it will not be functional anymore and compilation process will fail. To overcome this fragility and explicitly declare the intent of the interface as being functional,Java 8 adds special annotation @FunctionalInterface (all existing interfaces in Java library have been annotated with @FunctionalInterface as well). Let us take a look on this simple functional interface definition:
1
2??
3?
4
One thing to keep in mind:??do not break the functional interface contract and may be declared:
1
2??
3?
4?
5??
6
7
Lambdas are the largest selling point of Java 8. It has all the potential to attract more and more developers to this great platform and provide state of the art support for functional programming concepts in pure Java. For more details please refer to?.
2.2. Interface’s Default and Static Methods
Java 8 extends interface declarations with two new concepts: default and static methods.??make interfaces somewhat similar to traits but serve a bit different goal. They allow adding new methods to existing interfaces without breaking the binary compatibility with the code written for older versions of those interfaces.
The difference between default methods and abstract methods is that abstract methods are required to be implemented. But default methods are not. Instead,each interface must provide so called default implementation and all the implementers will inherit it by default (with a possibility to override this default implementation if needed). Let us take a look on example below.
01??
02
03
04?
05?
06
07
08?
09????
10
11?
12????
13
14?
15?
16
17
The interface?Defaulable?declares a default method?notRequired()?using keyword?default?as part of the method definition. One of the classes,?DefaultableImpl,implements this interface leaving the default method implementation as-is. Another one,?OverridableImpl?,overrides the default implementation and provides its own.
Another interesting feature delivered by Java 8 is that interfaces can declare (and provide implementation) of static methods. Here is an example.
1??
2
3? supplier ) {
4?
5
6
The small code snippet below glues together the default methods and static methods from the examples above.
1???
2?
3
4?
5?
6
7
The console output of this program looks like that:
1
2
Default methods implementation on JVM is very efficient and is supported by the byte code instructions for method invocation. Default methods allowed existing Java interfaces to evolve without breaking the compilation process. The good examples are the plethora of methods added to?java.util.Collection?interface:?stream(),?parallelStream(),?forEach(),?removeIf(),…
Though being powerful,default methods should be used with a caution: before declaring method as default it is better to think twice if it is really needed as it may cause ambiguity and compilation errors in complex hierarchies. For more details please refer to?.
2.3. Method References
Method references provide the useful syntax to refer directly to exiting methods or constructors of Java classes or objects (instances). With conjunction of??expressions,method references make the language constructs look compact and concise,leaving off boilerplate.
Below,considering the class?Car?as an example of different method definitions,let us distinguish four supported types of method references.
01???
02??? supplier ) {
03?
04
05?
06????
07?
08
09?
10???
11?
12