Java SE Tutorialshttps://wwconfirmedw.codejava.net/java-seSun, 05 May 2024 00:44:43 -0500Joomla! - Open Source Content Managementen-gbJava SE versions historyhttps://wwconfirmedw.codejava.net/java-se/java-se-versions-historyhttps://wwconfirmedw.codejava.net/java-se/java-se-versions-history

This article gives you an overview of all Java versions throughout its history, for Java Standard Edition (SE) Development Kit (JDK), from the first version to the latest one till date.

The latest version of Java is Java 22 or JDK 22 released on March, 19th 2024 (follow this article to check Java version on your computer). JDK 22 is a regular update of Java SE platform. And JDK 21 is currently the latest long-term support release (LTS), replacing JDK 17 which is the previous LTS of the Java SE platform.

From the first version released in 1996 to the latest version JDK 22 available to the public since March 2024, the Java platform has been actively being developed for more than 28 years. Many changes and improvements have been made to the technology over the years. The following table summarizes all versions of Java SE from its early days to the latest.

 

Java SE Version 

 

 

Version Number

 

Release Date

 

JDK 1.0

(Oak)

 

 

1.0

 

January 1996

 

JDK 1.1

 

 

1.1

 

February 1997

 

J2SE 1.2

(Playground)

 

 

1.2

 

December 1998

 

J2SE 1.3

(Kestrel)

 

 

1.3

 

May 2000

 

J2SE 1.4

(Merlin)

 

 

1.4

 

February 2002

 

J2SE 5.0

(Tiger)

 

 

1.5

 

September 2004

 

Java SE 6

(Mustang)

 

 

1.6

 

December 2006

 

Java SE 7

(Dolphin)

 

 

1.7

 

July 2011

 

Java SE 8

 

 

1.8 

 

March 2014

 

Java SE 9

 

 

9 

 

September, 21st 2017

 

Java SE 10

 

 

10 

 

March, 20th 2018

 

Java SE 11

 

 

11 

 

September, 25th 2018

 

Java SE 12

 

 

12

 

 

March, 19th 2019

 

Java SE 13

13

 

September, 17th 2019

 

 

Java SE 14

 

14 

  

March, 17th 2020

 

Java SE 15

 

15

 

September, 15th 2020

 

Java SE 16

 

16

 

March, 16th 2021

 

Java SE 17

 

17

 

September, 14th 2021

 

Java SE 18

 

18

 

 

March, 22nd 2022

 

 Java SE 19  

 

19

 

September, 20th 2022

 

Java SE 20

 

20

 

March, 21st 2023

 

Java SE 21 (LTS)

 

21

 

September, 19th 2023

Java SE 22

22

 

March, 19th 2024

 

From the table above we can see that the naming and the version number have been changing over times:

  • Versions 1.0 and 1.1 are named as JDK (Java Development Kit).
  • From versions 1.2 to 1.4, the platform is named as J2SE (Java 2 Standard Edition).
  • From versions 1.5, Sun introduces internal and external versions. Internal version is continuous from previous ones (1.5 after 1.4), but the external version has a big jump (5.0 for 1.5). This could make confusion for someone, so keep in mind that version 1.5 and version 5.0 are just two different version names for only one thing.
  • From Java 6, the version name is Java SE X.

Major versions were released after every 2 years, however the Java SE 7 took 5 years to be available after its predecessor Java SE 6, and 3 years for Java SE 8 to be available to public afterward.

Since Java SE 10,  new versions will be released very six months.

 

Related Topics:

 

If you want to dive deep into Java programming and become a software developer, I recommend you to learn this Java course on Udemy.

]]>
hainatu@gmail.com (Nam Ha Minh)Java SESat, 10 Mar 2012 08:18:14 -0600
Download and Install Java 11 (OpenJDK and Oracle JDK)https://wwconfirmedw.codejava.net/java-se/download-and-install-java-11-openjdk-and-oracle-jdkhttps://wwconfirmedw.codejava.net/java-se/download-and-install-java-11-openjdk-and-oracle-jdk

In this post, I will guide you to setup Java 11 on Windows operating system by downloading and installing Java Development Kit (JDK) version 11 from Oracle. You know, Oracle released Java 11 builds under two different licenses: Oracle JDK (commercial build with Oracle Technology Network license) and OpenJDK (open source build with GNU General Public license).

 

1. Download and Install Oracle JDK 11

Oracle JDK 11 is the first LTS (Long Term Support) Java Development Kit since Oracle changed Java release cadence to every 6 months. According to Oracle, JDK 11 will be supported (commercial support) until September 2026.

Head to Java SE Development Kit 11 Downloads page and choose the download file appropriate to your operating system. Oracle JDK 11 comes with installers for Linux (rpm and deb), macOS (dmg), Windows (exe) and archive files (tar.gz and zip).

For Windows, I recommend to download the file jdk-11.0.7_windows-x64_bin.exe. Note that you must have an Oracle account to be able to download JDK 11 installer. If not, creating one is free.

Run the downloaded file, and you will see the JDK 11 setup program appears:

jdk 11 setup

Just click Next twice to proceed installation with the defaults.

Note that Oracle JDK 11 does not update system environment variables, so you have to manually update PATH and/or JAVA_HOME after installation. Open Command Prompt window under administrator privilege and type the following command:

setx -m JAVA_HOME "C:\Program Files\Java\jdk-11.0.7"

If the PATH environment variable does not contain an entry to JAVA_HOME\bin, type the following command:

setx -m PATH "%JAVA_HOME%\bin;%PATH%"

Then open another command prompt window and type java –version, you will see:

java 11 version

That means you have successfully installed and configured Oracle JDK 11 on your computer.

You can also watch the video below:

https://www.youtube.com/watch?v=Wonb1ncRxbc

 

2. Download and Install OpenJDK 11

OpenJDK 11 is the open source distribution of JDK 11, licensed under GNU General Public License version 2 (GPLv2).

To download OpenJDK 11, you have to go to OpenJDK Archive download page. Then scroll down a little bit to find the version 11.0.2. OpenJDK is distributed in only zip or tar.gz file. For Windows, download the zip file for Windows 64-bit, i.e. openjdk-11.0.2_windows-x64_bin.zip file.

Extract the downloaded zip file to a directory. Then type the following command to update JAVA_HOME (in Command Prompt with administrator right):

setx -m JAVA_HOME "g:\JDK\OpenJDK\jdk-11.0.2"

Then open another command prompt and type java –version, you will see:

open jdk version

That means you have successfully setup OpenJDK 11 on your computer.

You can also watch the video below:

https://www.youtube.com/watch?v=p_ielKr3k7U

 

Related Tutorials:

]]>
hainatu@gmail.com (Nam Ha Minh)Java SEThu, 30 Apr 2020 07:00:19 -0500
Download and Install JDK 14 (OpenJDK and Oracle JDK)https://wwconfirmedw.codejava.net/java-se/download-and-install-jdk-14-openjdk-and-oracle-jdkhttps://wwconfirmedw.codejava.net/java-se/download-and-install-jdk-14-openjdk-and-oracle-jdk

This article is a guide to download and install OpenJDK and Oracle JDK for Java 14 on Windows operating system.

You know, Java Development Kit (JDK) 14 was released on March, 17th 2020 with two kinds of build: OpenJDK and Oracle JDK. OpenJDK is distributed under GPL license and Oracle JDK is distributed under Oracle Technology Network (OTN) license.

 

1. Download and Install OpenJDK 14

OpenJDK is a production-ready and open-source Java Development Kit, released under the GNU General Public License (GPL) version 2. That means you can freely use OpenJDK for personal, development and commercial use.

Go to the official download page of OpenJDK 14: https://jdk.java.net/14/

Then choose the appropriate package for your operating system (Linux, MacOS or Windows). Note that OpenJDK comes with only archive file (zip or tar.gz), no installer program. For instance, the archive file for Windows is openjdk-14_windows-x64_bin.zip (~189MB).

I strongly recommend you to verify the SHA256 checksum of the file downloaded. On Windows, type the certutil command like this:

certutil -hashfile openjdk-11.0.2_windows-x64_bin.zip SHA256

Then compare the hash code generated with the one provided on the website. If both are identical, it’s safe to install and use.

Then extract the archive file and update the PATH or JAVA_HOME system environment variable pointing to the installation directory of JDK 14 (Read the instruction here). For example:

JAVA_HOME=g:\JDK\OpenJDK\jdk-14

PATH=%JAVA_HOME%\bin;…

PATH= g:\JDK\OpenJDK\jdk-14\bin;…

Then type the java –version command to verify. You should see the following screen:

openjdk java version

 

This means OpenJDK has been installed successfully.

Watch the video:

https://www.youtube.com/watch?v=TjNuyFOPPZE

 

2. Download and Install Oracle JDK 14

Oracle JDK is a commercial build for Java Development Kit. Oracle JDK is free for personal and development use, but you have to pay license fee for commercial use via Java SE subscription program.

Click official Java SE download page to download Oracle JDK which is distributed in both archive (zip and tar.gz) and installer program (rpm on Linux, dmg on Mac and exe on Windows).

Choose the installer according to your operating system, e.g. Windows x64 Installer for Windows – the file name is jdk-14_windows-x64_bin.exe.

You should also verify the SHA256 checksum for the file downloaded, as described above. Then run the installer program. You will see the Java(TM) SE Development Kit 14 (64-bit) program appears as follows:

oracle jdk installer

Just click Next twice to install. The setup for Oracle JDK 14 using installer is quick and easy, but it doesn’t update the relevant system environment variables and do file associations. So you still need to manually update the JAVA_HOME and PATH (See the instruction here). For example:

JAVA_HOME=C:\Program Files\Java\jdk-14

Now in the command prompt, type the java –version command to check:

oracle jdk java version

You see, JDK 14 is now installed on your computer.

Watch the video:

https://www.youtube.com/watch?v=Roz0ASAa1cg

 

Next, you may need to configure your Eclipse IDE to experiment new language features in Java 14, follow these tutorials:

 

Learn more:

]]>
hainatu@gmail.com (Nam Ha Minh)Java SEFri, 20 Mar 2020 01:50:12 -0500
Download and Install Java 8 on Windowshttps://wwconfirmedw.codejava.net/java-se/download-and-install-java-8-on-windowshttps://wwconfirmedw.codejava.net/java-se/download-and-install-java-8-on-windows

In this article, I will guide you to download and install Java 8 (JDK 8) on Windows operating system. Though Java 8 is a quite old version (released in 2014), it is still in used by many organizations and preferred by many programmers. That’s why Oracle extends commercial support for JDK 8 until December 2030.

To download JDK 8, head to Java SE Development Kit 8 Downloads page and choose a download file suitable for your operating system. Oracle JDK 8 is distributed in archives (zip and targ.z) and installers (rpm for Linux, dmg for macOS and exe for Windows).

For Windows 10 x64-bit, choose to download the file jdk-8u251-windows-x64.exe file. You must login using an Oracle account (if you don’t have an Oracle account, then creating one is free).

I recommend you to verity the integrity of the downloaded by comparing SHA256 checksum of the file against the value published here. On Windows, type the following command:

certutil -hashfile jdk-8u251-windows-x64.exe sha256

If the generated SHA256 checksum value matches the one posted by Oracle, you’re safe to launch JDK 8 setup program. You will see this screen:

jdk 8 setup

Click Next twice to proceed the installation of Java 8. Then it also asks you to install JRE 8:

jre8 setup

Click Next to proceed and click Close when the setup done.

Note that Oracle JDK 8 setup automatically updates the PATH system environment variable so if you type java –version command in a Windows Command prompt, you would see this:

java 8 version

That means you have successfully installed JDK 8 on your computer. For development with Java 8, you should configure JAVA_HOME as described in this guide.

For visual steps, you can watch the following video:

https://www.youtube.com/watch?v=XsdvQD_SDvw

 

Related Tutorials:

]]>
hainatu@gmail.com (Nam Ha Minh)Java SETue, 05 May 2020 06:56:21 -0500
Download and Install JDK 12 (OpenJDK and Oracle JDK)https://wwconfirmedw.codejava.net/java-se/download-and-install-jdk-12https://wwconfirmedw.codejava.net/java-se/download-and-install-jdk-12

In this post, I will guide you how to download and install Java Development Kit (JDK) for Java SE 12 from the official source, on Windows operating system.

You know, Oracle releases JDK 12 on 19 March 2019 with two kinds of build: OpenJDK and Oracle JDK. Note that JDK 12 was superseded and not recommended for production use. Only developers should use JDK 12 to debug issues in old systems.

 

1. Download and Install OpenJDK 12

OpenJDK 12 is an open-source distribution of Java SE 12. It is licensed under GNU General Public License version 2 (GPLv2), which means you can use OpenJDK for personal, development and commercial use.

Since OpenJDK 12 was superseded, you can download it from Archived OpenJDK Releases page.

Scroll down a little bit and you will see the last build of OpenJDK 12 is 12.0.2:

openjdk 12 archive

As you can see, OpenJDK 12 is distributed in zip and tar.gz archives for Windows, macOS and Linux. Download the file suitable for your operating system. For Windows, it is openjdk-12.0.2_windows-x64_bin.zip file.

It’s strongly recommended to verify integrity of the downloaded file by comparing its SHA256 checksum with the one mentioned on OpenJDK website. On Windows, type the following command to generate SHA256 checksum for the downloaded file:

certutil -hashfile openjdk-12.0.2_windows-x64_bin.zip sha256

If the checksums are equal, it’s safe to proceed.

Extract the zip file to a directory and update the JAVA_HOME environment variable pointing to the new JDK directory, using the following command:

setx -m JAVA_HOME "G:\OpenJDK\jdk-12.0.2"

You need to run the Windows command prompt under administrator privilege to execute this command. And make sure the PATH environment variables includes the entry JAVA_HOME\bin.

Then open another command prompt window and type java –version to check:

openjdk 12 java version

This means you have successfully installed OpenJDK 12.

Watch the video: 

https://www.youtube.com/watch?v=25RQtazHOhM

 

2. Download and Install Oracle JDK 12

Oracle JDK 12 was a commercial build for Java SE 12 but it was superseded. Oracle JDK 12 is not recommended for production use. Only programmers should use it to debug issues in old systems.

To download Oracle JDK 12, you need to go to Oracle Java Archive Downloads page. Scroll down a little bit and click the link Java SE 12 Archive Downloads. You can see Oracle JDK 12 is provided with both archive files (zip and tar.gz) and installers for Linux, macOS and Windows.

For Windows, you can download the installer program jdk-12.0.2_windows-x64_bin.exe. Note that you must have an Oracle account and sign in to download.

Click the downloaded file to launch the installer program for Oracle JDK 12. You will see the following wizard appears:

oracle jdk 12 setup

Just click Next twice to start installing. It will be very quick, simple and easy.

Then type the following command to update the JAVA_HOME environment variable (requires administrator privilege):

setx -m JAVA_HOME "C:\Program Files\Java\jdk-12.0.2"

Also make sure to include JAVA_HOME\bin in the PATH variable:

setx -m PATH "%PATH%;%JAVA_HOME%\bin";

Finally, open a new command prompt and type java –version to verify:

oracle jdk 12 java version

That means you have successfully installed Oracle JDK 12.

Watch video:

https://www.youtube.com/watch?v=aghEF1ak8sY

 

Related Tutorials:

]]>
hainatu@gmail.com (Nam Ha Minh)Java SETue, 07 Apr 2020 22:50:08 -0500
Download and Install JDK 13 (OpenJDK and Oracle JDK)https://wwconfirmedw.codejava.net/java-se/download-and-install-jdk-13-openjdk-and-oracle-jdkhttps://wwconfirmedw.codejava.net/java-se/download-and-install-jdk-13-openjdk-and-oracle-jdk

In this post, I will guide you how to download and setup JDK 13 on Windows operating system for both OpenJDK and Oracle JDK builds – so you can experiment the new language features in Java 13 like switch expressions, text blocks, etc.

Note that both OpenJDK and Oracle JDK made by Oracle, but they are distributed with different licenses.

 

1. Download and Install OpenJDK 13

OpenJDK 13 is a reference implementation of Java SE 13 which was officially release on September, 17th 2019. OpenJDK is free for personal, development and commercial use under GNU General Public License (GPLv2) without technical support.

Head to the official download page of OpenJDK 13: http://jdk.java.net/java-se-ri/13

You can see there are two binary builds: one for Oracle Linux and one for Windows 10 x64. For Windows, you will download the file openjdk-13+33_windows-x64_bin.zip. (~186 MB).

You should verify integrity of the downloaded file by comparing SHA256 checksum of the zip file with the one published on the website. On Windows, run this command:

certutil -hashfile openjdk-13+33_windows-x64_bin.zip SHA256

Then compare the checksum code generated with the SHA256 code on the website. If both are equal, you can proceed.

OpenJDK doesn’t have an installer, so you need to extract the downloaded zip file and update the system environment variables accordingly (see the instructions here):

JAVA_HOME = G:\JDK\OpenJDK\jdk-13

PATH = %JAVA_HOME%\bin;…

Or:

PATH = G:\JDK\OpenJDK\jdk-13\bin;…

Then type java –version command in a command prompt window to verify:

open jdk 13 version

You see, it prints ‘openjdk version “13” 2019-09-17’ which means you have successfully installed OpenJDK 13. Next, you may need to configure Eclipse IDE to run under the new JDK (see how). You can also watch the video below:

https://www.youtube.com/watch?v=bxE5ZMiSAdI

 

2. Download and Install Oracle JDK 13

Oracle JDK is a commercial build for Java Development Kit with paid technical support for long term. But you can use Oracle JDK for personal and development use at no cost.

To download Oracle JDK 13, go to its official Java SE 13 download page. You can see Oracle JDK comes with installers for different operating systems, as well as compressed archive files. For Windows, choose to download Windows x64 Installer.

Then you will download the jdk-13.0.2_windows-x64_bin.exe file (~160 MB), and verify SHA256 checksum for the downloaded file using this command:

certutil -hashfile jdk-13.0.2_windows-x64_bin.exe sha256

Run the Oracle JDK 13 installer program and you will see the following screen:

oracle jdk 13 setup

The setup for Oracle JDK 13 is simple, quick and easy. Just click Next twice to proceed the installation. However, you still have to update the JAVA_HOME or PATH system environment variables (see how) pointing to the new JDK:

JAVA_HOME = C:\Program Files\Java\jdk-13.0.2

And then type java –version command to check:

oracle jdk 13 version

Now, Oracle JDK 13 has been installed successfully on your computer.

Next, you may need to configure Eclipse IDE to run under the new JDK.

Watch the video:

https://www.youtube.com/watch?v=2kCnWHCIzFE

 

Learn more:

]]>
hainatu@gmail.com (Nam Ha Minh)Java SEThu, 02 Apr 2020 00:57:20 -0500
Java 15 - Download and Install OpenJDK 15https://wwconfirmedw.codejava.net/java-se/download-and-install-openjdk-15https://wwconfirmedw.codejava.net/java-se/download-and-install-openjdk-15

Java 15 was out, available to the public on September 15th 2020. In this article, I will help you get started experimenting Java 15 with OpenJDK – an open-source distribution of Java SE Development Kit: download and install OpenJDK 15 on Windows operating system.

OpenJDK is licensed under GNU General Public License version 2, which means developers can use it for personal and commercial use.

 

1. Download OpenJDK 15

OpenJDK is distributed as a zip archive for Windows (no installer program). So head over to OpenJDK 15 homepage to download it, or click this direct OpenJDK 15 download link. You will get a file named openjdk-15_windows-x64_bin.zip downloaded onto your computer.

After download, I strongly recommend you to use the certutil in Windows to verify the file’s SHA256 checksum – by running this command:

certutil –hashfile openjdk-15_windows-x64_bin.zip SHA256

Compare the outputted value with the one published here. If both matches, it’s safely to use the downloaded zip file.

 

2. Install OpenJDK 15

Extract the downloaded zip file to a separate directory on your computer, say in D:\OpenJDK15, then you will get the home directory of OpenJDK 15 is D:\OpenJDK15\jdk-15.

Then you need to update the JAVA_HOME and PATH environment variables as follows:

JAVA_HOME= D:\OpenJDK15\jdk-15

PATH=%JAVA_HOME%\bin;…

You can follow this video on how to set environment variables for Java.

Then open a new command prompt window, type the command java –version to check. You should see this screen:

java version openjdk 15

This means you have successfully installed OpenJDK 15 and configured the environment properly.

To see the download and installation of OpenJDK 15 in action, watch the video below:

https://www.youtube.com/watch?v=-WKcMw_d26I

 

Learn more:

]]>
hainatu@gmail.com (Nam Ha Minh)Java SEThu, 24 Sep 2020 00:05:34 -0500
Java 15 – Download and Install Oracle JDK 15https://wwconfirmedw.codejava.net/java-se/download-and-install-oracle-jdk-15https://wwconfirmedw.codejava.net/java-se/download-and-install-oracle-jdk-15

This article will guide you how to download and install Oracle JDK 15 – a commercial distribution of Java Development Kit – to get started with Java 15 development on Windows operating system.

Oracle JDK 15 is licensed under Oracle Technology Network (OTN) license. You can use it freely for personal and non-commercial use. For commercial use, you need to pay for a license in terms of Java SE subscription program.

 

1. Download Oracle JDK 15

Oracle JDK 15 is distributed in archive files (tar.gz and zip) and installer programs for popular operating systems like Windows, Linux and Mac. For Windows, I recommend you to download a setup program which makes the installation easy (automatic update environment variables and file associations).

So head over to Oracle’s official Java SE Download page, and choose to download Windows x64 Installer. Then you will get a file named jdk-15_windows-x64_bin.exe downloaded onto your computer.

After download, I recommend you to verify the integrity of the downloaded file by running the following command:

certutil –hashfile jdk-15_windows-x64_bin.exe SHA256

Then compare the outputted SHA256 value with the one published on Oracle’s website here. If two checksums match, you can safely run the setup program.

 

2. Install Oracle JDK 15

Double click the downloaded EXE file to launch setup program for Oracle JDK 15. You will see this dialog appears:

Oracle JDK 15 setup

Just click Next two times to proceed the installation with default settings. It will install Oracle JDK 15 quickly. And finally click Close to complete the installation.

Now, open a new command prompt window and type the command java –version, you will see the version information of JDK like this:

oracle java version 15

This means you have successfully installed Oracle JDK 15 on your computer. Note that the installer has updated the environment variables JAVA_HOME and PATH behind the scene. So it’s very convenient.

To see the download and installation of Oracle JDK 15 in action, you can follow the video below:

https://www.youtube.com/watch?v=1YdE5zhiBLs

 

Learn more:

]]>
hainatu@gmail.com (Nam Ha Minh)Java SEThu, 24 Sep 2020 01:58:02 -0500
Java 14: What’s New, Deprecated and Removedhttps://wwconfirmedw.codejava.net/java-se/java-14-new-featureshttps://wwconfirmedw.codejava.net/java-se/java-14-new-features

Java 14 was released on March 17th 2020 with some interesting new features added to the language – notably the finalization of switch expression enhancements, which leads to addition of the yield keyword. Another cool stuff is that, JDK 14 comes with a new packaging tool called jpackage– allows programmers to create native installer for Java applications.

And like previous Java releases, performance for the Java Virtual Machine (JVM) is continuously improved with several updates for garbage collectors. Among that, something is deprecated for removal in future as well as some other things are actually removed in JDK 14.

Table of content:

1. New Language Features in Java 14

2. Updates for Tools and APIs in Java 14

3. JVM Performance Improvements in JDK 14

4. What’s Deprecated in JDK 14

5. What’s Removed in JDK 14

Now, let’s dive into the details for what’s new, deprecated in removed in Java 14.

 

1. New Language Features in Java 14

Enhancements for switch expressions become standard, whilst Text blocks, Records and Pattern matching for instanceof are still in preview.

Switch Expression Enhancements:

The switch-case expression in Java is well-known for its verbosity for multiple-case fall through. Consider the following method:

public void dayOfMonth(int month) {
	switch (month) {
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			System.out.println("this month has 31 days");
			break;
		case 2:
			System.out.println("this month has 28 or 29 days");
			break;
		case 4:
		case 6:
		case 9:
		case 11:
			System.out.println("this month has 30 days");
			break;
		default:
			System.out.println("invalid month number");
	}
}

This is clearly very verbose. Now with Java 14’s enhancements for switch expression, we can rewrite this method as follows:

public static void dayOfMonth(int month) {
	switch (month) {
		case 1, 3, 5, 7, 8, 10, 12 -> System.out.println("this month has 31 days");

		case 4, 6, 9, 11 -> System.out.println("this month has 30 days");

		case 2 -> System.out.println("this month probably has 28 days");

		default -> System.out.println("invalid month number");
	}
}

You see, it’s much clearer and more readable, isn’t it?

Moreover, the switch block can return a value to be used in an expression like this:

int days = switch(month) {
	case 1, 3, 5, 7, 8, 10, 12 -> 31;

	case 4, 6, 9, 11 -> 30;

	case 2 -> 28;

	default -> 0;
};

If the code in a case is a block, we can use the yield keyword to return the value, for example:

int days = switch(month) {
	case 1, 3, 5, 7, 8, 10, 12 -> 31;

	case 4, 6, 9, 11 -> 30;

	case 2 -> {
		System.out.print("Enter year: ");
		Scanner scanner = new Scanner(System.in);
		int year = scanner.nextInt();

		if (year % 4 == 0)
			yield 29;
		else
			yield 28;
	}

	default -> 0;
};

So, since Java 14, yield becomes the keyword used in switch expression to return a value. Read the JDK Enhancement Proposal JEP 361 that covers the full detailed of enhancements for switch expression.

For more code examples with detailed explanation, read this post Java 14: Switch Expressions Enhancements Examples.

 

Text Blocks (preview):

This feature allows programmers to write lengthy String literals in a way that is more readable and effortlessly. Consider the following String literal:

String html = "<html>\n" +
		"    <head>\n" +
		"        <title>Homepage</title>\n" +
		"    </head>\n" +
		"    <body>\n" +
		"        <h1>This is the homepage</h1>\n" +
		"    </body>\n" +
		"</html>\n";

From Java 14, you can rewrite this String literal as follows:

String html = """
	  <html>
	      <head>
		  <title>Homepage</title>
		  </head>
		  <body>
			  <h1>This is the homepage</h1>
		  </body>
	  </html>
	  """;

You see, it looks much more naturally, right? A text bock starts with 3 double quotes follow by a new line character, and ends with 3 double quotes.

With text block, you don’t have to escape special characters like new lines or double quotes. Note that the Java compiler will automatically trim the leading spaces in a text block, making it more convenient for programmers.

Read the JEP 368 that describes text block in full details.

Update: Text blocks become a standard feature in JDK 15. Learn more: Understand Text Blocks Feature in Java 15?

 

Records (preview):

Records are a new kind of type declaration in the Java language. It simplifies the coding of classes that merely act as “data carrier” i.e. domain classes or POJO classes. Consider the following class:

public class Book {
	private String title;
	private String author;

	public Book(String title, String author) {
		this.title = title;
		this.author = author;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getTitle() {
		return this.title;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public String getAuthor() {
		return this.author;
	}
} 

Now, with the new record type, you can simply write:

record Book (String title, String author) { }

Then the Java compiler will automatically generate appropriate fields, constructor, getter and setter methods. In addition, a record also implements equals(), hashCode() and toString() methods implicitly.

Read the JEP 359 for more information about Records features.

 

Pattern matching for instanceof operator (preview):

Another new feature was proposed for the Java language is enhancement for the use of instanceof operator. Consider a very familiar use case below:

if (obj instanceof String) {
	String s = (String) obj;
	// use s
}

Here, the statement that casts the obj object to a type of String is somewhat verbose because when the if statement evaluates to true, the type of the object is indeed String. Now with Java 14, we can improve the instanceof statement as follows:

if (obj instanceof String s) {
	// we use s directly here
}

As you can see, it eliminates the verbose cast statement. More advanced, we can add expression to the right like this:

if (obj instanceof String s && s.length() > 5) {
	// use String s if its length > 5
}

This would make the use of instanceof operator more flexible and concise. Read the JEP 305 that covers the greater details of pattern matching for instanceof.

Note that the preview features require the switches --enable-preview and --source 14 when compiling and running. Preview features need further feedback and improvements until they become finalized in future releases of JDK. 

 

2. Updates for Tools and APIs in Java 14

Another interesting feature in JDK 14 is the introduction of a new packaging tool, among some updates for JDK APIs.

New Packaging Tool (Incubator)

JDK 14 shipped with jpackage tool that allows programmers to package a Java application into platform-specific package that includes all of the necessary dependencies. This tool will generate installer program for your Java application in native formats: msi and exe on Windows, pkg and dmg on macOS, and deb and rpm on Linux.

I’ve experimented this tool for creating a Windows installer for my Java application, using this command:

jpackage --name CodeJavaForm --input dist --main-jar GUIFormExamples.jar 
--main-class examples.ContactEditor --type msi 
--win-dir-chooser --win-menu --win-shortcut

And I got a nice installer program that looks like this:

jpackage setup win

So I think for the first time in history, programmers can create native installer programs for their Java applications with ease. Read the JEP 343 for more information about packaging tool in JDK 14.

 

Java IO: Non-Volatile Mapped Byte Buffers

This update adds new JDK-specific file mapping modes so that the FileChannel API can be used to create MappedByteBuffer instances that refer to non-volatile memory. Check the JEP 352 for details.

 

Helpful NullPointerExceptions

Perhaps, NullPointerException is the most well-known error in Java programming. However, the way which the JVM reports NullPointerException is ambiguous in non-trivial cases. Given two classes A and B as follows:

class A { int i; }
class B { int j; } 

And a statement that uses object references of A and B like this:

a.i = b.j;

If NullPointerException throws at this line, JVM will terminate with the following error message: 

Exception in thread "main" java.lang.NullPointerException
        at MyProgram.test(MyProgram.java:14)
        at MyProgram.main(MyProgram.java:6)

It points exactly the line where the exception occurred. But wait, which variable is null in this case, a or b??? So programmers have to debug the program or write some checking code to know which a or b is null.

The good news is, JDK 14 now computes the null-detail message to help programmers quickly and exactly identify which variable is null. To enable null-detail message, we must specify the following switch when running Java program:

java -XX:+ShowCodeDetailsInExceptionMessages MyProgram

Then JVM will produce more meaningful error message like this:

Exception in thread "main" java.lang.NullPointerException: Cannot assign field "i" because "this.a" is null
        at MyProgram.test(MyProgram.java:14)
        at MyProgram.main(MyProgram.java:6)

I think this is a cool feature that saves programmer’s time in debugging their code. Read the JEP 358 for more details.

 

Foreign-Memory Access API (Incubator)

This feature introduces an API to allow Java programs to safely and efficiently access foreign memory outside of the Java heap. These three interfaces are introduced: MemorySegment, MemoryAddress and MemoryLayout. Read the JEP 370 for greater details.

 

Java Flight Recorder (JFR) Event Streaming

For better consumption of data generated by the Java Flight Recorder (JFR), JDK 14 introduces an API that allows Java programs to subscribe to JFR’s data events asynchronously, for continuous monitoring of the Java Virtual Machine. Read the JEP 349 for more information about this feature.

 

3. JVM Performance Improvements in JDK 14

JVM performance is continued to be improved in this release. The Garbage First (G1) collector performance is improved by implementing the NUMA-aware memory allocation. NUMA stands for Non-Uniform Memory Access – a modern memory architecture on multi-processor machines.

In addition, the Z Garbage Collector (ZGC) is now available for macOS and Windows operating systems - but in experimental phase. ZGC is a scalable low latency garbage collector since JDK 11.

 

4. What’s Deprecated in JDK 14

JDK 14 deprecates the two garbage collection algorithms, namely Parallel Scavenge and Serial Old because they are very little used but requires a significant amount of maintenance effort.

JDK 14 also deprecates ports to Solaris/SPARC, Solaris/x64, and Linux/SPARC – which means there will be no JDK builds for Solaris operating system running on SPARC and x64 architecture, and no JDK builds for Linux operating system running on SPARC architecture.

 

5. What’s Removed in JDK 14

JDK 14 removes the Concurrent Mark Sweep (CMS) garbage collector. CMS has been intented to be replaced by G1 since JDK 6. That also allows the JDK development team to focus on ZGC and Shenandoah garbage collectors.

JDK 14 also removes the pack200 and unpack200 tools, and the Pack200 API in the java.util.jar package. These tools and API were used by developers to compress and uncompress their JAR files – but the major consumer of Pack200 – the JDK itself – no longer needs it.

To help you easily understand how to use new language features in Java 14, I've also published the following video. Watch it now:

https://www.youtube.com/watch?v=p7pr1-N3oug

 

References:

 

You may be also interested in:

]]>
hainatu@gmail.com (Nam Ha Minh)Java SEMon, 23 Mar 2020 21:15:14 -0500
What are New Features in JDK 15?https://wwconfirmedw.codejava.net/java-se/java-15-new-featureshttps://wwconfirmedw.codejava.net/java-se/java-15-new-features

JDK 15 has been released to the public Java developer community since September 15th 2020, as of 6-month release cadence. In this article, I would like to share with you some new features in Java 15 that are most important to developers.

 

1. Sealed Classes and Interfaces (preview)

This language feature allows programmers to specify exactly known subtypes of a class or interface. In other words, restrict the extension of a superclass to only some known subclasses – prevent arbitrary (unknown) subclasses. Let’s take an example as below:

public sealed class Shape permits Rectangle, Circle, Triangle { }

Here, the Shape class permits exactly only 3 subclasses that are Rectangle, Circle and Triangle. These subclasses are known at compile time, and no other classes can extend the Shape class.

And a subclass of a sealed class should be declared either final, sealed or non-sealed. For example, the Rectangle class is declared as follows:

public final class Rectangle extends Shape { }

Here, no classes can extend the Rectangle class because it is declared as final.

And the Circle class is declared as follows:

public sealed class Circle extends Shape permits Sphere { }

This means the Circle class is sealed and it permits Sphere as the only one subclass.

And the Triangle class is declared as below:

public non-sealed class Triangle extends Shape { }

Here, the Triangle class is non-sealed, which means it opens extension for any unknown classes.

The similar rules applied for sealed interfaces. This feature will introduce 3 potential new keywords for the Java language: sealed, permits and non-sealed. Sealed classes and interfaces are still in preview mode in Java 15, which requires further feedback from developer community before it becomes a standard feature in future release of Java.

For greater details of sealed classes, you can read JEP 360 on java.net.

 

2. Hidden Classes

Java 15 introduces hidden classes, which are classes that cannot be used directly by other classes. Hidden classes are intended for use by frameworks that generate classes at run time and use them indirectly via reflection.

If you use frameworks like Spring and Hibernate, you probably know that they generate lots of dynamic classes at run time. For example, Hibernate generates dynamic classes as proxies of entity classes and Spring generates dynamic classes for transaction management and repositories implementations.

I think the main motivation behind hidden classes is to generate short-live dynamic classes which can be unloaded completely from JVM when they are no longer used – avoid unnecessary increased memory footprint.

Study JEP 371 for detailed specification of hidden classes.

 

3. Pattern matching for instanceof (second preview)

This feature was introduced in JDK 14 and it is still under review in JDK 15. In Java, programmers are used to use the instanceof operator in a very verbose way:

if (obj instanceof String) {
    String s = (String) obj;
    // use s
}

The statement that casts the object obj to the variable s of type String is unnecessarily verbose. But now, programmers don’t have to write that statement anymore, like this:

if (obj instanceof String s) {
    // s can be used directly here
}

Furthermore, we can add expressions to the instanceof operator as shown below:

if (obj instanceof String s && s.length() > 10) {
    // use String s if its length > 10
}

So I think this feature will make the use of instanceof operator easier, more flexible and more powerful. And hope it will become standard feature in Java 16. For details, you can read the JEP 375 page.

 

4. Text Blocks (Standard)

Another frustrating issue when writing Java code is the declaration of String literals that span on multiple lines. Let’s take a classic example:

@Query("SELECT p FROM Product p WHERE p.name LIKE %?1% "
		+ "OR p.shortDescription LIKE %?1% "
		+ "OR p.fullDescription LIKE %?1% "
		+ "OR p.brand.name LIKE %?1% "
		+ "OR p.category.name LIKE %?1%")
public Page<Product> findAll(String keyword, Pageable pageable);

Though IDEs can help writing multi-line String literals easily, the concatenation operators and escape sequences make the String difficult to read and also maintenance is cumbersome.

Feel the pain of developers, Java 15 finally supports text blocks feature that allows programmers to write multi-line String literals much easier – almost avoid the need of concatenation and escape sequences. The above code can be rewritten using text blocks as shown below:

@Query("""
	SELECT p FROM Product p WHERE p.name LIKE %?1% \
	OR p.shortDescription LIKE %?1% \
	OR p.fullDescription LIKE %?1% \
	OR p.brand.name LIKE %?1% \
	OR p.category.name LIKE %?1%
	""")
public Page<Product> findAll(String keyword, Pageable pageable);

Note that the backward slash character (\) tells Java compiler to continue the String literal on the next line (hence not inserting new line characters).

So I think this is the most useful feature in Java 15. We now can embed SQL, HTML, JSON, XML, etc in Java code easily and avoid the maintenance headache.

Read this article to understand how to use text blocks in depth.

 

5. Record (second preview)

Java is also very well-known for its verbosity when it comes to “data carrier” classes (e.g. domain model/entity/POJO). Let’s take a classic entity class written in Java:

public class Product {
	private Integer id;
	private String name;
	private float price;

	public Product() {
	}
	
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public float getPrice() {
		return price;
	}

	public void setPrice(float price) {
		this.price = price;
	}

	@Override
	public int hashCode() {
		// ...
	}

	@Override
	public boolean equals(Object obj) {
		// ...
	}
	
	@Override
	public String toString() {
		// ...
	}	
}

As you can see, the field’s getters and setters, and equals(), hashCode(), toString() methods are cumbersome and it will be frustrating when the fields and entity classes growing.

Now with the record feature, the above lengthy class can be declared as simple as below:

record Product (Integer id, String name, float price) { }

I think this feature will be very helpful for developers. However, it is still under review (second preview) in Java 15, and it may become standard feature in Java 16.

Besides the new features above, JDK 15 also includes the following updates, for your reference:

 

Edwards-Curve Digital Signature Algorithm (EdDSA)

EdDSA is a modern cryptography algorithm which provides improved security and performance compared to other algorithms. JDK 15 implements EdDSA into its crypto API, so programmers can use EdDSA without having to use a third-party library.

 

Reimplement the Legacy DatagramSocket API

The DatagramSocket API was implemented in the early days of JDK when IPv6 was still under development so it has several issues. That’s why JDK 15 replaces the underlying implementation of this API with simpler and more modern implementations that are easy to maintain and debug.

 

Foreign-Memory Access API (Second Incubator)

This API was first introduce in JDK 14 and is still in incubator in JDK 15. It aims at providing an API that allows programmers to safely and efficiently access the memory area that is outside of Java heap.

 

Remove the Nashorn JavaScript Engine

JDK 15 finally dropped Nashorn – a Javascript engine running on JVM. That means developer is no longer able to write and run Javascript on JDK 15 and future releases. The suggested alternative is GraalVM which supports latest Javascript specification and provides better performance.

 

Deprecate RMI Activation for Removal

RMI is a very old technology which could be used in the early days of Internet and World Wide Web. Today, RMI is no longer suitable and almost dead so it’s naturally to be removed from JDK.

And as usual, new JDK release always includes performance improvements for Java Virtual Machine and garbage collector. For the complete list of features in JDK 15 with full detailed description, I recommend you to visit the OpenJDK 15 project page.

To see the code of new features in Java 15 in action, watch the video below:

https://www.youtube.com/watch?v=dmeLQBoxNFA

 

You may be also interested in:

 

]]>
hainatu@gmail.com (Nam Ha Minh)Java SESun, 11 Oct 2020 07:44:15 -0500