Cool features you missed from Java 8 to 14

Image for post
Image for post

Java is In a different cycle releasing features from very short cycles which are considered for some folks not ideal. IMHO it’s great because it faster innovation and make the language alive. There are some folks saying that java is just copying features from other languages but I dont see issues in that, actually this is good. Java is a very old language 1995, however, it still used a lot at the enterprise and used to run most critical programs like databases, search engines, distributed log, and message brokers, HTTP servers and several other key components that we use day by day. Today for this blog post I want to share some features that you might miss from java 9 to java 14. There are some interesting features which could help you every single day.

All the Source code you will see is this blog post was generated using JDK 14 and JShell which will be covered later on this very post. JShell is an amazing feature IMHO the best feature in Java Stream support in Java 8. Lately, I’m doing lots of work around Rust and using Rust in production, however, my background was made by using Java in Production, and still, I have a system with Java in production.

Var

Var feature comes from JDK 11 and supports you to create a local variable with less ceremony. What is cool is that you dont need to worry about types, it makes similar like javascript.

Image for post
Image for post

Objects.nonNull

Added in Java 8, few folks know it. It’s an easy way to check for null.

Image for post
Image for post

Although the feature is cool it’s it does not cover blank String, so you might still need to use Apache Commons.

Jshell

JShell was added to Java 9. IMHO is the best feature from java 8 to 14. It’s also known as a REPL(Read Evaluate Print and Loop) which is famous in functional programming languages like Clojure, Scala, Haskell, and now Java. So it’s useful to play with the language and also experiment code with less formality and speed.

Image for post
Image for post

Switch Expressions

Switch expressions are like Pattern Matcher that exist in functional programming languages like Scala, Haskell, Rust. In java, it’s possible to assign the result of switch expression to a variable.

Image for post
Image for post

Text Blocks

Text Blocks are multi-line strings. This makes easier to avoid scaping with strings and automatically formats the string in a predictable way.

Image for post
Image for post

Helpful null pointer exception

NPE was always complicated in Java. Null Pointer is considered a million dollars mistake. Languages like Rust avoid null and is much better. Now when an NPE happens there is a much better exception message letting you know what happens and how to fix the NPE. It would be better to remove NPE but the impact would be much bigger at this point.

Convenience Methods for Collections

Instantiate and populate collections in java was always verbose and a bit wanting. But this gov fixed in Java 9. Now it’s super easy to and convenient to instantiate ad populate collections.

Image for post
Image for post

I Love this feature, Scala had it for a Long time. It makes the developer experience much much better.

Records

One of the best features that Java stole from Scala. Records are like case classes in Scala.

Image for post
Image for post

Records are cool because they generate access methods, toString(), equals() and hashCode().

Why Records? Just for syntactical slugger? No. Records are great for Immutable classes.

So far this is small changes but slowly Java is getting back on track. I wish the changing flow continues and Java keeps adding features, making the language live and hopefully new frameworks and libraries will appear.

Cheers,
Diego Pacheco

Originally published at http://diego-pacheco.blogspot.com on March 26, 2020.

Written by

Brazilian, Software Architect, SWE(Java, Scala, Rust, Go) SOA & DevOps expert, Author. Working with EKS/K8S. diegopacheco.github.io (Opinions on my own)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store