Blog, Trixi

Geecon – What’s New for Java 8

In March 2014 new version of Java has been released. In this speech Jim Weaver has introduced new features of Java 8 to us. Let’s take short review of his talk.

Lambda expressions

Lambda expression is not a new concept in programming world. There is a lot of programming languages containing similar mechanism and from my own experience I know that I’ve missed it in Java. With lambda expression you can skip creating anonymous inner classes and make your code plain and more readable.

I’m not going to explain how lambda expressions work. Oracle published a lot of texts about it. This article seems to be a good start: http://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html. Just note that if you’ll see something like this

p -> {
     p.someMethod()
}

it is a lambda expression :)

There is one more new feature related to lambda expressions and it is called double colon notation. Sometimes lambda expression does nothing but calls a method. In this case you can use this shortcut:

p::someMethod

This is the same lambda expression as in the previous example.

You can use four kinds of double colon method reference:

  • Reference to a static method
    ContainingClass::staticMethodName
    
  • Reference to an instance method of a particular object
    ContainingObject::instanceMethodName
    
  • Reference to an instance method of an arbitrary object of a particular type
    ContainingType::methodName
    
  • Reference to a constructor
    ClassName::new
    

For more information you can check following link http://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html

Interface default and static methods

Java 8 is narrowing the gap between abstract class and interface by introducing interface default and static methods. Default method is method implementation in interface. If you do not implement method of the interface in your implementing class Java will use default one.

Problem could be when your class implements two or more interfaces with the same method signature but different default implementation. In this case code won’t compile.

Interface can now contain static methods as well.

We can tell that by using these two features you can tell that interface is something more than abstract class. You can have method implementation in interface but you can’t have class implementing more than one abstract class.

You can find more information here: http://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html

Stream API

New and revolutionary (from my point of view) feature are streams. This will significantly change the way we are coding in Java. Forget the io streams which are well known to every Java programmer. This is something completely different and I think everybody should learn to use streams in Java 8. A lot of functionalities of stream API you can replace by common for-loops and if statements but there is a big difference – new notation is really REALLY more readable and comprehensive.

1 -> [0 – n intermediate operations] -> [terminal operation]

From every collection you can create stream by calling simple stream method on this collection. This is called source of the stream. Source could be followed by one or more intermediate operations. These operations could filter, map, transform etc. items of the stream. Finally stream expression is ended by terminal operation. This is operation which constructs result actually.  All previous methods are lazy, they are called only when terminal operation was called.

double average = employees
.stream()
.filter(e -> e.isActive())
.mapToInt(Employee::getAge)
.average()
.getAsDouble();

List of employees is source of the stream. Filter and mapping method are intermediate methods. Average is terminal operation. And yes, this simple code calculates average age of active employees.

One big advantage of the streams is that they could be processed by parallel processes (programmer should request it by creating parallel stream instead of common one). One thing I’m afraid of is debugging and searching for errors in code. It could be really annoying as you are in fact not looping through collection and you can’t check why some items were filtered and some not. But only future shows if this is good approach.

There is a lot of possible intermediate operations and terminal operations, just check java 8 API (http://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html) for complete list. See http://docs.oracle.com/javase/tutorial/collections/streams/index.html for more information about streams.

Optional Type

Java 8 has new class – Optional. Basically it is wrapper which may or may not contain value. By using Optional instead of direct usage of our class we can signal that this variable could contain null value and should be checked.  This can dramatically reduce number of NullPointerExceptions in our applications.

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

Conclusion

There are far more improvements and new features in Java 8. But I’ve chose those which really impressed me. Jim’s talk was great and I can recommend visiting some of his lectures he’ll take in future. I think the changes in Java 8 come really handy and we will use a lot of them very often.

Posted in programming


Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>