demo-java-x icon indicating copy to clipboard operation
demo-java-x copied to clipboard

Demonstrates Java 9-18's new features

Java X Feature Demo

Demonstrates most features that were introduced after Java 8. They are listed below with links that take you to the demos in this repo, the JEPs responsible for introducing the feature, and to other sources if available. These are the categories:

  • Language Changes
  • New APIs
  • Updated APIs
  • Tooling
  • Runtime
  • Internals

Circled numbers like ⑰ indicate which Java version introduced a feature or a change. A circled X ⓧ indicates an incubating or previewing feature that is not yet standardized - it can be experimented with but is subject to change.

For a more practical approach to many of these features, including some quick code metrics, check my Java After Eight repo. You can find more from me on nipafx.dev as well as on YouTube and Twitch. To get in touch, follow me on Twitter (DMs are open) or join my Discord.

Setup

This project requires at least the most recent Java release, at times even early-access builds of upcoming versions. You can get OpenJDK builds for both directly from jdk.java.net, although I prefer to use SDKMAN to install and manage them. The project can be built with Maven, i.e. mvn package.

There are several ways to execute demos:

  • If your IDE is up to speed (which may require preview versions or additional plugins), you can execute most demos straight from there.
  • If a feature is contained in a single class, you can also use single-source file execution and just call java $SOURCE_FILE.
  • You can always build with mvn package and then run any specific class with ...
    ava -p target/java-x.jar -m dev.nipafx.demo.java_x/$CLASS
    
    ... where $CLASS is the fully qualified name of the class you want to execute.

Depending on the demo you may have to add additional command line flags - this should be listed in the respective file.

For some features, you have to run the .sh scripts in this repo's root directory to see them in action. If that's necessary, the feature list below mentions it. For that to work, you need to ensure that a java binary from the respective JDK version is available on your path - again, I use SDKMAN for that.

Language Changes

Module System

The module system is too big to demo here. Check out the j_ms tag on my blog (for example to find this handy cheat-sheet), the dedicated demo project, a project with solutions to common issues, or my book The Java Module System.

New APIs

If an API that was introduced in Java 9+ was later updated, the update is listed in the next section.

Updated APIs

  • ⑯ (server) socket channels: Unix domain socket support (article, JEP 380)
  • ⑯ HTTP/2 client: copying a request
  • Stream:
    • ⑯: mapMulti (article 1, 2)
    • ⑫: teeing collector (article)
    • ⑩: to-unmodifiable collector
    • ⑨: takeWhile, dropWhile, iterate, ofNullable (article)
  • String:
    • ⑮: formatted,
    • ⑫: indent, transform
    • ⑪: lines, repeat, strip
  • @Serial (Javadoc)
  • formatting numbers
  • I/O:
    • ⑫: Files::mismatch
    • ⑪: null streams/readers, Path::of, Files::readString/writeString
  • CompletableFuture:
    • ⑫: exceptionallyCompose, exceptionallyComposeAsync
    • ⑨: lots of additions
  • Predicate::not
  • Pattern::asMatchPredicate
  • collections
    • ⑪: Collection::toArray overload
    • ⑩: List/Set/Map::copyOf
  • Optional:
    • ⑪: isEmpty
    • ⑩: orElseThrow
    • ⑨: or, ifPresentOrElse, stream (article)
  • ⑩ version API: feature(), interim(), update()
  • OS processes (JEP 102)

A few changes have their own articles (in which case they are linked), but many don't. Some of them are show-cased in these posts, though:

Tooling

  • ⑱ external snippets in Javadoc (video, Maven how-to, JEP 413):
    • referencing class (and how-to)
    • referenced demo class
    • build configuration
  • jwebserver, a simple web server (video, JEP 408)

Runtime

  • helpful NullPointerExceptions:
    • ⑯: enabled by default
    • ⑭: introduced (JEP 358)
  • ⑬⑫⑩ application class-dara sharing (article, JEP 310, JEP 341, JEP 350)
  • ⑪ single-source-file execution (article, JEP 330):
    • getting started
    • echo class
    • proper script (run with cat echo-haiku.txt | ./echo)
  • unified logging (article, JEP 158)
  • ⑨ multi-release JARs: classes and the script (JEP 238)
  • ⑨ platform logging: classes and the script (JEP 264)

Internals

  • new version string schema (JEP 223)
  • UTF-8 property files (JEP 226)
  • reserved stack areas (JEP 270)
  • DRGB implementations for SecureRandom (JEP 273)
  • string compaction (JEP 254)