New Features in Java 9 to 15 in 5 mins

TribalScale Inc.
Apr 9 · 7 min read

by Shubham, Agile Software Engineer

Java is trying to shed its old skin by coming up with a more modern, concise and intuitive approach of organizing your code, working with JSON etc. This can already be seen in their release cycle strategy. Earlier, Java releases were driven by prominent features like Lambda Expressions or Generics but after releasing Java 9, they changed the release cycle to a much faster one. Instead of releasing only major increments twice a decade, they are releasing Java updates twice a year and a LTS (long term support release) version every three years. Therefore, it is possible for developers to familiarize themselves with some of the major features that are released. In this blog I focus on some important features relevant to developers.

Photo by Fotis Fotopoulos

Java 9:

  1. JShell: Java has always been a tough coding languages to learn for starters with a steep learning curve. To write even a simple program to capitalize a string you need to know the meaning of keywords like class, public, static return, and main method.

The popularity of languages like Python and Javascript can be attributed to the fact that they have a relatively smaller learning curve.

To help users overcome this hurdle and catch up with its competitors Java came up with JShell, an interactive console similar to other languages mentioned above.

It has a built-in autocomplete feature (also called Tab-completion feature) which allows you to minimize typing by just pressing the Tab key.

Now, the above code can be written in just three lines.

It’s a very good addition to Java not only for newbies but also for experienced programmers as it gives them a place to quickly try out snippets of code.

2. Private Methods in Interface: My first reaction: why do we need it? Well, when you couple this feature with default functions in interfaces introduced in Java 8 then it will start making more sense. For example, take a look at the code below:

Can you notice the code duplication? Step 1 & Step 3 are performed by all the methods. If we see the code below:

Now we can appreciate how private methods help to share common code between non-abstract methods (default methods). Private methods can only be used in the default methods of the interface itself, so they themselves cannot be default methods. Their use is limited to being helper methods for default methods of the interface.

3. Creating Unmodifiable Collections: This is another feature which helps us get rid of the verbosity in Java in order to create immutable data structures. The below code would be familiar to you all especially when writing test cases.

Java 9 offers convenient static factory method ‘of(…)’ on the List, Set, and Map interfaces,

The collection created is immutable (it cannot be modified). There are 12 overloaded versions of this method — eleven with 0 to 10 parameters and one with var-args. It’s a nice one line syntactic sugar which makes the life of a programmer easier.

Java 10:

Var keyword: I have always had a love-hate relationship with typed languages. I like the sense of security a typed language offers, however, I hate the inflexibility and verbosity that comes with them. For example in the code below, it’s a pain to write the type of the ‘map’ variable in the javaIsVerbose() function.

Let me introduce my new friend: the new ‘var’ keyword in Java 10 which fixes the verbosity part to a great extent. The same line above can now be written as:

The Java compiler has become intelligent enough to infer the type of the local variables on its own, making the code readable, and less verbose without sacrificing the security of type safety.

Java 11:

Single Source File Launch: This feature is again aimed to make learning Java easier. This feature provides the ability to run a Java single-file source code directly without any compilation, avoiding tedious steps that were involved previously to run just a simple Hello World program.

To run a Hello World program, we had to run the following two commands:

With this new feature we can omit the first step of compilation and can straight away run the source file:

When we combine JShell with this new feature we get a great beginner’s learning tool set.

Photo by Procreator UX Design Studio

Java 12:

Switch Expression: It’s never fun to write nested or multiple if-else statements, so we have switch statements to our rescue. But it does come with its own caveats. What if you miss the break statement? The worst part is your code still compiles without warnings or errors!

Java 12 fixes this issue and adds a bunch of other new features such as fall-through semantics, compound form, exhaustiveness, and expression form to switch expressions.

You would have observed that ‘:’ has been replaced with ‘->’ just like lambda functions. But, don’t confuse yourself by treating it as one. Also, you don’t need to write break statements on each case like before, the compiler takes care of that. The scoping can now be used in a case level, allowing for a cleaner code and some more flexibility. Java 12 allows switch statements to return values and list multiple case labels on the same line.

Java 13:

Text Blocks: We developers often work with multi line string values such as JSON, HTML, XML, or regular expression (regex) data. Especially, when working with web apps we deal a lot with JSON format. If I had to initialize a JSON in Java it will look something like this:

This is cumbersome and hard to read. The other problem is that various characters cannot be represented directly, but need to be escaped, such as “ or \. This makes your strings even harder to read and work with.

Here’s how working with a multi line JSON value becomes simpler with text blocks:

Declaring it in a text block within “”” removes all the visual clutter due to escape sequences and concatenation operators, and the JSON value can be edited with ease. The best part is that it’s still a String and all the operations of the string can be performed on it as before.

Java 14:

Data Classes: Most often our application reads data from a database or writes to it. We create data classes commonly known as Entities and override methods like toString(), equals() and hashCode() from the Object class and write getters and setter methods.

Take for example an Employee data class below:

All the above boilerplate code can be auto generated using an IDE. Therefore, Java 14 came up with the ‘record’ keyword and have automatic getters, a constructor,equals() method etc.

By default, the compiler creates a bunch of methods for a record, like instance methods to store its state, constructors, getters (no setters-the intent to the data be immutable), and overrides toString(), equals() and hashCode() method from the Object class. This can be easily done by using ‘record’ keyword as below:

For more details you can refer to this really informative article.

Java 15:

Sealed classes: One of the best features of Java was code reuse through inheritance. But, if you make your class public any other class can extend it and if you declare a class final then no other class can extend it. Therefore, making class (that needs to be inherited at some point) either public can be too generous and making it final can be too restrictive.

Java 15 comes to our rescue by providing a more fine-grained inheritance control via sealed classes and interfaces. Sealed classes enlists the classes that can extend it using the ‘permits’ keyword.

By sealing a class, you can specify which classes are permitted to extend it and prevent any other arbitrary class from doing so. The main motivation behind sealed classes is to have the possibility for a super class to be widely accessible but not widely extensible.

If we club sealed classes with records and pattern matching then we can find that they support a more data-centric form of programming.

Conclusion

So, these were some of the new exciting features released which will help new programmers to easily learn the language, as well help experienced developers to write more concise and elegant code. These features reaffirm the fact that Java is evolving and here to stay.

Shubham is an Agile Software Engineer who is enthusiastic and motivated. He has a curious mind and believes Severless is going to change the way softwares are built. He enjoys the outdoors, and you can find him on a hike or in a canoe on a hot sunny day.

TribalScale is a global innovation firm that helps enterprises adapt and thrive in the digital era. We transform teams and processes, build best-in-class digital products, and create disruptive startups. Learn more about us on our website. Connect with us on Twitter, LinkedIn & Facebook!

TribalScale

A digital innovation firm with a mission to right the future.

TribalScale

Not a massive development shop, VC firm, or design agency. But a unique group of skilled individuals, all feeding on one another’s talent. Empowering businesses to grow their success.

TribalScale Inc.

Written by

A digital innovation firm with a mission to right the future.

TribalScale

Not a massive development shop, VC firm, or design agency. But a unique group of skilled individuals, all feeding on one another’s talent. Empowering businesses to grow their success.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

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