The last version of the Java Development Kit was released last September and in this article, we will go through the installation of it on an Ubuntu Server. But before that, let’s review what are the new features that it comes with:
- Dynamic CDS Archives
- Uncommit Unused Memory
- Reimplement the Legacy Socket API
- Switch Expressions (Preview)
- Text Blocks (Preview)
Switch Expressions and Text Blocks — Preview Features?
Switch Expressions and Text Blocks were added as preview features. That means that the feature is not permanent, in new versions of the JDK, it can exist in a different form or not at all.
To enable these features, in case you are using IntelliJ IDEA, version 2019.2 onwards already has support for preview features. For Eclipse, you can follow the steps in this guide. And in case you run it from a terminal, you can enable the preview features with the following commands
javac --enable-preview --release 13 MyApp.java
java --enable-preview MyApp
If you want to read further about preview features, you can find a good article about it here
Text Blocks
Text Blocks allows us to create multiline Strings like below
String myString = """
This
is
an
example""";
This feature increase code readability when you have SQL statements or HTML within your code. This looks pretty similar to Python multiline strings, isn’t it?
Switch Expressions
Switch expressions are an alternative to Switch statements, this feature was introduced in Java 12, but is now improved for Java 13 using user feedback. In the new version, switch expressions are replacing the keyword break by yield. The yield statement exits the switch and returns the result of the current branch, similar to a return.
int x = switch (choice) {
case 1, 2, 3:
yield choice;
default:
yield -1;
};
Dynamic CDS Archives
The goal of Class Data Sharing(CDS) is to shorten the start times of Java applications by storing certain information about classes in CDS archives. This data can then be loaded at runtime and used by several JVMs.
Until Java 10, however, the shared archives were only accessible for the Bootstrap ClassLoader. Starting with Java 10, CDS was extended by Application Class Data Sharing (AppCDS). AppCDS enables the built-in system and platform class loader as well as user-defined class loaders to access the CDS archives. Class lists are required to create the CDS archives in order to identify the classes to be loaded.
Previously, these class lists had to be determined by trial runs of the application to determine which classes were actually loaded during execution
The updated feature is intended to improve the usability of AppCDS and eliminate the need for users to do trial runs to create a class list for each application
Uncommit Unused Memory
As part of this new feature, the ZGC (Z Garbage Collector) was enhanced to return unused heap memory to the operating system.
Reimplement the Legacy Socket API
Replaces the underlying implementation used by the java.net.Socket and java.net.ServerSocket APIs with a simpler and more modern implementation that is easy to maintain and debug. The new implementation will be easy to adapt to work with user-mode threads, a.k.a. fibers, currently being explored in Project Loom.
Installation steps
To start with, let’s download the needed files for the installation. You can get them manually from this site, or directly from the Ubuntu box running the commands below:
cd /tmp
curl -O https://download.java.net/java/GA/jdk13.0.1/cec27d702aa74d5a8630c65ae61e4305/9/GPL/openjdk-13.0.1_linux-x64_bin.tar.gz
After we have the needed files, let’s create a new directory where our installation will reside and extract the files there:
sudo mkdir /opt/jdk13.0.1
sudo tar xzvf openjdk-13*tar.gz -C /opt/jdk13.0.1 --strip-components=1
Lastly, we need to add the JAVAHOME environment variable and add it to the PATH. You can do that creating a new configuration file within /etc/profile.d. You can name the file as you want, for example, envvars.sh.
To create this file, use your favorite text editor, an example command using vi editor:
sudo vi /etc/profile.d/env_vars.sh
Then add the following two lines to it, and save it.
export JAVA_HOME=/opt/jdk13.0.1
export PATH=$PATH:$JAVA_HOME/bin
Load the file changes:
source /etc/profile.d/env_vars.sh
And that’s it. To verify that our server is running OpenJDK13:
java -version
openjdk version "13.0.1" 2019-10-15
OpenJDK Runtime Environment (build 13.0.1+9)
OpenJDK 64-Bit Server VM (build 13.0.1+9, mixed mode, sharing)