Java 8 is packed full of some really exciting features at both the JVM and language level. While some of the features initially envisioned for this release got scoped out or pushed out to release 9, there are literally dozens of new features. Many of the new additions are under-the-hood improvements either at the compiler, JVM or help-system level. As such, while we may benefit from them, there’s nothing we need to actively do (other than install Java 8 of course) to enjoy them.
Having said that, let’s look at 5 features that we feel are an absolute must for you to know about:
1. Lambda expressions
Even if we really didn’t want to go mainstream here, there’s little doubt that from a developer’s perspective, the most dominant feature of Java 8 is the new support for Lambda expressions. This addition to the language brings Java to the forefront of functional programming, right there with other functional JVM-based languages such as Scala and Clojure.
We’ve previously looked into how Java implemented Lambda expressions, and how it compared to the approach taken by Scala. From Java’s perspective this is by far one of the biggest additions to the language in the past decade.
At minimum, it’s recommended you become familiar with the Lambda syntax, especially as it relates to array and collection operations, where Lambdas have been tightly integrated into the core language libraries. It is highly likely that you’ll start seeing more and more code like the snippet below in both 3rd party and within your organization’s code.
* A pretty efficient way of grouping a collection by the value of a specific class field.
2. Parallel operations
With the addition of Lambda expressions to arrays operations, Java introduced a key concept into the language of internal iteration. Essentially as developers we’re used to use loop operations as one of the most basic programming idioms, right up there with if and else.
The introduction of Lambda expressions turned that paradigm around, with the actual iteration over a collection on which a Lambda function is applied now carried out by the core library itself (i.e. internal iteration).
You can think of this as an extension of iterators where the actual operation of extracting the next item from a collection on which to operate is carried out by an iterator. An exciting possibility opened by this design pattern is to enable operations carried out on long arrays such as sorting, filtering and mapping to be carried out in parallel by the framework. When dealing with server code that’s processing lengthy collections on a continuous basis, this can lead to major throughput improvements with relatively little work from your end.
Here’s the same snippet as above, but using the framework’s new parallel processing capabilities –
* It’s a fairly small change that’s required to make this algorithm run on multiple threads.
4. New date / time APIs
The complexity of the current native Java library API has been a cause of pain for Java developers for many years. Joda time has been filling this vacuum for years now, and with Java 8. An immediate question that arose early on was why didn’t Java 8 adopt Joda as its native time framework. Due to what was perceived as a design flaw in Joda, Java 8 implemented its own new date / time API from scratch. The good news is that unlike Calendar.getInstance(), the new APIs were designed with simplicity in mind, and clear operations to operate on manipulated values in both human readable and machine time formats.
5. Concurrent accumulators
One of the most common scenarios in concurrent programming is updating of numeric counters accessed by multiple threads. There have been many idioms to do this over the years, starting from synchronized blocks (which introduce a high level of contention), to read/write locks to AtomicInteger(s). While the last ones are more efficient, as they rely directly on processor CAS instructions, they require a higher degree of familiarity to implement the required semantics correctly.
With Java 8 this problem is solved at the framework level with new concurrent accumulator classes that enable you to very efficiently increase / decrease the value of a counter in a thread safe manner. This is really a case where it’s not a question of taste, or preference – using these new classes in your code is really a no-brainer.
——————————-Section 2 —————————————————–
Lambdas lambdas lambdas. That’s all you ever hear about when talking about Java 8. But that’s just one part. Java 8 has a lot of new features – some are powerful new classes and idioms, while others are just things that should have been there from the get-go.
I wanted to go over ten new features which I consider to be little gems definitely worth knowing about. There are bound to be at least one or two you’d like to try, so let’s get going!
1. Default Methods
A new addition to the Java language, you can now add method bodies to interfaces (called default methods). These methods are implicitly added to every class which implements the interface.
This enables you to add functionality to existing libraries without breaking code. That’s definitely a plus. The flip side is that this seriously blurs the line between an interface, which is meant to serve as a contract, and a class which serves as its concrete implementation. In the right hands this can be an elegant way to make interfaces smarter, avoid repetition and extend libraries. In the wrong hands, we’ll soon be seeing interface methods querying this and casting it to a concrete type.Shivers….
2. Process Termination
Launching an external process is one of those things you do half-knowing you’ll come back to debug it when the process crashes, hangs or consumes 100% CPU. The Process class now comes equipped with two new methods to help you take control of unruly processes.
The first one, isAlive(), lets you easily check if the process is still up without having to wait for it. The second and more powerful one is destroyForcibly() which lets you forcibly kill a process which has timed-out or is no longer necessary.
Now here’s something to get excited about. Nobody likes to synchronize code. It’s a sure-fire way of reducing your app’s throughput (especially under scale), or worse – cause it to hang. Even so, sometime you just don’t have a choice.
There are plenty of idioms to synchronize multi-threaded access to a resource. One of the most venerated ones is ReadWriteLock and its associated implementations. This idiom is meant to reduce contention by allowing multiple threads to consume a resource while only blocking for threads that manipulate it. Sounds great in theory, but in reality this lock is sloooow, especially with a high number of writer threads.
This got so bad that Java 8 is introducing a brand new RWLock called StampedLock. Not only is this lock faster, but it also offers a powerful API for optimistic locking, where you can obtain a reader lock at a very low cost, hoping that no write operation occurs during the critical section. At the end of the section you query the lock to see whether a write has occurred during that time, in which case you can decide whether to retry, escalate the lock or give up.
This lock is a powerful tool and deserves a complete post by itself. I’m giddy with excitement about this new toy – well done!
Additional reading here.
4. Concurrent Adders
This is another little gem for anyone working on multi-threaded apps. A simple and efficient new API for reading and writing to counters from multiple threads, in a way that’s even faster than using AtomicIntegers. Pretty darn cool!
5. Optional Values
Oh, NullPointers, the bane of all Java developers. Maybe the most popular of all exceptions, this has been around since the dawn of time. Or at least 1965.
Borrowing from Scala and Haskell, Java 8 has a new template named Optional for wrapping references that may be null. It’s by no means a silver bullet to end nulls, but more a means for an API designer to signify at the code level (vs. the documentation) that a null value may be returned or passed to a method, and the caller should prepare for it. As such, this will only work for new APIs, assuming callers do not let the reference escape the wrapper where it can be unsafely dereferenced.
I have to say I’m pretty ambivalent about this feature. On one hand, nulls remain a huge problem, so I appreciate anything done on that front. On the other hand I’m fairly skeptical this’ll succeed. This is because employing Optional requires continuing company-wide effort, and with little immediate value. Unless enforced vigorously, chances are this will be left at the side of the road.
More about Optional here.
6. Annotate Anything
Another small improvement to the Java language is that annotations can now be added to almost everything in your code. Previously, annotations could only be added to things like class or method declarations. With Java 8 annotations can be added to variable and parameter declarations, when casting to a value to specific type, or even when allocating a new object.
This is part of a concentrated effort (along with improvements to the Java doc tools and APIs) to make the language more friendly towards static analysis and instrumentation tools (e.g FindBugs). It’s a nice feature, but much likeinvokeDynamic introduced in Java 7, its real value will depend on what the community does with it.
7. Overflow Operations
Now here’s a set of methods which should have been a part of the core library from day one. A favorite hobby of mine is to debug numeric overflows when ints exceed 2^32, and go on to create the nastiest and most random of bugs (i.e. “how did I get this weird value?”).
Once again, no silver bullet here, but a set of functions to operate on numbers that throw when overflow in a less forgiving way than your standard +/ * operator which implicitly overflow. If it was up to me I’d have this be the default mode for the JVM, with explicit functions that allow arithmetic overflow.
8. Directory Walking
Iterating the contents of a directory tree has long been one of those go-to google searches (in which case you should probably be using Apache.FileUtils). Java 8 has given the Files class a face-lift, with ten new methods. My favorite one is walk() which creates a lazy stream (important for large file systems) to iterate the contents of a directory structure.
9. Strong Random Generation
There’s no shortage of talk nowadays about password and key vulnerability. Programming security is a tricky business and prone to mistakes. That’s why I like the new SecureRandom.getinstanceStrong() method which automatically picks the strongest random generator available to the JVM. This reduces the chances of you failing to get, or defaulting to a weak generator, which will make keys and encrypted values more susceptible to hacking.
Java 8 introduces a complete new date time API. This is fairly understandable, as the existing one isn’t very good. Joda has essentially been the go-to Java date time API for years now. Still, even with the new API one big problem remains – there’s a TON of code and libraries using the old API.
And we all know they’re here to stay. So what do you do?
For this Java 8 has done something pretty elegant, adding a new method to the Date class called toInstant() which converts it to the new API. This enables you to make a quick jump to the new API, even when working with code that uses the old Date API (and will continue to do so in the foreseeable future).