Java 11 in a Nutshell

Hakan Eröztekin
Analytics Vidhya
Published in
5 min readOct 11, 2020
Photo by Blake Wisz on Unsplash

Java is one of the world’s most widely used and in-demand programming languages. Since it’s arrival in 1995, Java kept improving over time. With its frequent release cycle in the recent years, it requires a bit more effort to keep up with the new releases.

We will start with a small Q&A section to clear out the questions and then discuss the Java 11 changes with “in a nutshell” approach. In approximately 15 minutes, you will get familiar with what’s changed with Java 11. There is also a slide version of the article as well.

Let’s begin.

First of all, why are there so many releases?

Oracle used to release a new version every 2 to 3 years. However, it took great effort for companies to adopt too many changes at once.

Oracle then adopted continuous release approach and announced 6 months release period. Since Java 9, Oracle releases a new Java version with relatively minor changes every 6 months.

What is LTS?

A long term version is tend to be an industry standart for several years. Even if new releases come after it, it still gets updates. For example, Java 8 is released in 2014, still getting updates in 2020 and will get extended support until 2030.

So which version should I upgrade to?

Oracle strongly recommends switching to Java 11 or a latest version. The section below from Wikipedia is important,

Oracle (and others) “highly recommend that you uninstall older versions of Java”, because of serious risks due to unresolved security issues. Since Java 9 (and 10) is no longer supported, Oracle advises its users to “immediately transition” to Java 11 (Java 15 is also a non-LTS option) - wiki

Java 11 is the latest LTS, until the release of Java 17 in September 2021. It’s backwards compatible with Java 8 (we’ll talk about the details). Also it brings noteworthy features and improvements in addition to Java 8.

Is Java 11 free to use?

In short, yes.

The confusion comes from licensing. There are two types of licenses. OpenJDK is free to use, while Oracle JDK provides long-term support for a price. The binaries of these are almost identical so you can use OpenJDK with ease. If you’re interested here is the complete list of differences for Java 11.

Let’s continue with what Java 11 brings to the table.

Overview of New Features

  1. HTTP API
  2. Launching Single-File Java Program
  3. New Library Methods for Strings, Collections and Files
  4. Performance and Security Improvements
  5. Other Enhancements
  6. Removed and Deprecated Features

1. HTTP API

HTTP API is a Java library to execute HTTP requests.

motivation

To replace HttpURLConnection which was introduced with Java 1.1 in 1997, and is difficult to use and maintain.

features

  • Supports HTTP/1.1 and HTTP/2 protocols
  • Supports common HTTP methods like GET, POST, PUT, DELETE
  • Can handle synchronous and asynchronous communication

how to use

  1. Create, initialize and configure HttpClient
  2. Create and initialize HttpRequest
  3. Pass HttpRequest to HttpClient, execute the request and retrieve HttpResponse
  4. Handle HttpResponsefor your purposes

example

Http Api Example

2. Launching Single-File Java Program

Java 11 supports execution of single file Java program with a single command.

details

  • Enables beginner friendly Java development and high speed prototyping.
  • It has to be a single-file program only (with one or more classes).
  • We can’t use external dependencies.

how to use

Let’s assume our file name is MyProgram.java, to execute it, we only need to use java MyProgram.java.

3. New Library Methods for Strings, Collections and Files

Java 11 added some useful methods for the mentioned libraries for convenience.

strings api

  • Repeat a string: Repeat a string n times with String.repeat(Integer). Example: " ".repeat(10)
  • Check empty/whitespace: Check if string is empty or whitespace only. String.isBlank(). It’ll throw NullPointerExceptionif the string is blank.
  • Remove whitespaces: Remove leading String.stripLeading(), trailing String.stripTrailing() or both whitespaces String.strip()
  • Process multiline string: Convert multiline /n string to stream of lines with String.lines()

collections api

  • Convert collection to an array: This process made easier with Java 11. For example, converting a list of students to string array would be performed with students.toArray(String[]::new)

optional and predicate.

  • Negating a predicate : We can filter out the elements not having a certain condition with Predicate.not(myCustomPredicate).

files api

  • Easier file read/write: Thanks to Files.readString(path) and Files.writeString(path, charSequence, options) we can achieve this operations.

4. Performance and Security Improvements

performance Improvements

  • G1 Garbage Collector Improvements: G1 was introduced in Java 7 and became the default GC in Java 9. With Java 11, G1 is improved for high performance. Java 11 improves pause times (client waiting for application response caused by GC) by up to 60% (source).
  • ZGC: An experimental but scalable and low-latency GC. It guarantees maximum pause time of 10 ms. ZGC is great for applications memory consuming applications that handle a lot of data.
  • Epsilon: An experimental but low-overhead GC. It’s a good fit for short living applications (executing a single command then closing).

security Improvements

  • The recent Transport Layer Security (TLS) improved HTTPS performance and security. Java 11 supports the TLS 1.3 protocol.

5. Other Enhancements

  • Using var in lambdas: We've met var with Java 10, now we can use it in lambdas. Advantage of this feature is now we can annotate lambda variables (eg. @NotNull)
  • Adoption of Unicode 10: Java 11 uses Unicode 10, an upgrade from Java 10 with Unicode 8. That includes 16,018 new characters and 10 new scripts.
  • Nested access control: Inner class can access all private variables and methods of Outer class (eg. Outer.name).
  • Java Flight Recorder: JFR is a profiling and diagnostics (resource usage etc.) tool. We can also use Java Mission Control to visualize data provided by JFR.

6. Removed and Deprecated Features

removed features

Java 11 is backwards compatible with Java 8. So you can swiftly change from Java 8 to 11. If you use some of the packages below, all you need to do is add the replacement dependency to your project.

Also, Applets and Java Web Start are completely removed without a replacement. If you’re looking for Java desktop application development, check out JavaFX.

deprecated features

  • Nashorn JavaScript Engine: Since it’s hard to maintain compatibility with rapid changing of ECMAScript, it’s now deprecated.

conclusion

Java 11 is the latest LTS release and introduced beneficial features and improvements on top of previous releases. A new HTTP client, new library methods for strings, collections, predicates and files are introduced. Further, garbage collector performances are optimized, along with two new garbage collectors, ZGC and Epsilon.

If you’re into learning all the aspects of Java in a nutshell, feel free to check out my Java in a Nutshell article.

Happy coding!

--

--