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.

Blog_treasure box__

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.

3. StampedLocks

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 like invokeDynamic 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.

10. Date.toInstant()

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).

Are there any features you think are missing and need to be here, or disagree with our analysis? Let us know in the comments section – that’s what it’s there for! 🙂

This post is also available in Spanish.

Duke_8 copy

5 Features In Java 8 That WILL Change How You Code – read more


Tal is the CTO of OverOps. Tal has been designing scalable, real-time Java and C++ applications for the past 15 years. He still enjoys analyzing a good bug though, and instrumenting code. In his free time Tal plays Jazz drums.
  • Lukas Eder

    1. Default Methods

    Ehm. Who exactly hasn’t heard of this feature? 😉 I would’ve expected something more subtle as the first point…

    • Tal Weiss

      Hi Lukas,

      True, I really debated whether to start with something big or small. Hope you did find some of the other 9 a bit more exotic 🙂

      • Lukas Eder

        Yes, specifically the concurrent adders and the strong random generation were news to me!

        • Tal Weiss

          Cool! 🙂

    • Peter Verhas

      Yeah, that was the first thing coming up to my mind 🙂

  • bondolo

    The @NonNull annotation is not included in JSR 308 or Java 8. It is possible to write one and use it more places the classic JCIP or Guava @NonNull annotation but it’s not currently included. JSR 305, if it ever gets finished is where @NonNull would appear.

    • Tal Weiss

      Hey Bondolo,

      Thanks for the note. I updated the item to focus on the foundational piece where Java 8 now allows us to annotate almost anything in the code. Hopefully NonNull will see the light of day soon!

      • Lukas Eder

        Hopefully NonNull will see the light of day soon!

        Well, if Mike Duigou, a.k.a. bondolo reads your posts, you might try your luck convincing him directly! 🙂

        • Tal Weiss

          I have to say this with zero degree of cynicism that it’s truly an honor to have devs of such caliber reading and commenting on (and correcting) posts.

  • Eyal Golan

    How about multiple annotations of the some kind for the same element?

    • Tal Weiss

      Hey Eyal,

      True, that’s also part of the extended annotation capabilities introduced in Java 8. Good stuff!

  • Javin Paul

    Hello Tai, NIce post. Though I wouldn’t say I never heard of Default Methods 🙂 , there are definitely couple of them which makes it time well spent.

    • Tal Weiss

      Thanks Javin! I thought I’d start with a big one and go more eclectic from there.

  • Peter Lawrey

    My signed ints keep overflowing before 2^32 or even at 2^31 😉

    • Tal Weiss

      Lol 🙂

  • Antonel Ernest Pazargic

    A feature I like is CompletableFuture.
    It is (somehow) like scala features.

    • Tal Weiss

      Thanks Antonel, cool feature indeed!

      • Antonel Ernest Pazargic

        I highly recommend the following presentation about Future (scala and java 8), promises (JS) and Reactive Extensions, a non-blocking way of consuming asynchronous services. It explains how blocking nature of the java 7 Future prevents scalability because of the get() lock.

        Another useful java 8 features are: Nashorn and Compact profiles (IoT)

        • Tal Weiss

          Awesome! thanks for sharing.

  • George Herson

    enjoyed, thanks. its info has already been useful for me.
    someone should correct the “Published at DZone with permission of its author, Chen Harel” byline at (or the byline here).

  • Dot Net Training in chennai

    Hi this is Rajesh i am having 3 years of experience as a php developer and i am certified. i have knowledge on OOPS concepts in php
    but dont know indepth. After learning Dot Net will be enough to get a good career in IT with good package? and i crossed Dot Net training in chennai website where someone
    please help me to identity the syllabus covers everything or not??

    thanks, Rajesh

  • Dot Net Training in chennai

    Hi this is Rajesh i am having 3 years of experience as a php developer and i am certified. i have knowledge on OOPS concepts in php but dont know indepth. After learning Dot Net will be enough to get a good career in IT with good package? and i crossed Dot Net training in chennai website where someone please help me to identity the syllabus covers everything or not??

    thanks, Rajesh

  • sindhu Khan

    It is nice blog and is informative.we provide the authorized training of websphere in our is used to get placement assurance.


  • Training Coimbatore
  • Gayathri

    New and useful information about java 8

    Hope Tutors

  • anusha smile

    Superb explanation on Java 8.Java Training in Tambaram

  • Deepika Analy

    Wow, Excellent post. This article is really very interesting and effective.The article you have shared here very awesome. I really like and appreciated your work. I read deeply your article, the points you have mentioned in this article are useful.

    Android Training
    Android Training in Chennai

  • JPMEduSolutions

    Great informative about Java. Thanks Best IT Software Training Institute