Java 8 vs Java 9
In this article, we’re going to present Java 8 vs Java 9.
1. Introduction
Java is an object-oriented programming language and first released in 1995. Java 8 is the first long-term-support (LTS) version and released in 2014. The current LTS version is 11. Java 9 is released in 2017. According to this article, Java 8 is the most popular version. The most significant new feature in Java 8 is the lambda expression. The most noticeable new feature in Java 9 is the Java Platform Module System.
Most Significant Features | Java Example Link | |
Java 8 | Lambda Expression Stream API | https://examples.javacodegeeks.com/java-8-api-tutorial/ |
Java 9 | Module JShell | https://examples.javacodegeeks.com/how-to-download-and-install-java-9/ |
In this example, I will show the key differences in Spring Tools Suite (STS) for Java 8 and Java 9.
2. Technologies Used
- OpenJDK 11
- Maven 3.6
- STS 4.10
3. STS IDE
Prior to Java 9, a Java application groups classes into a package. Java 9 application can define a module which groups packages and specifies which packages are exposed outside of the module. In this step, I will outline the STS IDE support for Java 9.
3.1 Java Build Path
STS IDE provides four tabs for Java Build Path prior to Java 9. See Figure 1 for Java 8 build path settings.
STS IDE adds a new tab – Module Dependencies to support Java 9. See Figure 2 for Java 9 build path settings.
As you seen at Figure 2, there are several buttons to update the Modulepath and Classpath.
4. Java Commands
4.1 –list-modules option
Java add –list-modules option to show all the modules packaged with installed JDK.
java –list-modules
C:\MaryZheng\sts_4_10_ws\jdk9-demo>java --list-modules java.base@11.0.2 java.compiler@11.0.2 java.datatransfer@11.0.2 java.desktop@11.0.2 java.instrument@11.0.2 java.logging@11.0.2 java.management@11.0.2 java.management.rmi@11.0.2 java.naming@11.0.2 java.net.http@11.0.2 java.prefs@11.0.2 java.rmi@11.0.2 java.scripting@11.0.2 java.se@11.0.2 java.security.jgss@11.0.2 java.security.sasl@11.0.2 java.smartcardio@11.0.2 java.sql@11.0.2 java.sql.rowset@11.0.2 java.transaction.xa@11.0.2 java.xml@11.0.2 java.xml.crypto@11.0.2 jdk.accessibility@11.0.2 jdk.aot@11.0.2 jdk.attach@11.0.2 jdk.charsets@11.0.2 jdk.compiler@11.0.2 jdk.crypto.cryptoki@11.0.2 jdk.crypto.ec@11.0.2 jdk.crypto.mscapi@11.0.2 jdk.dynalink@11.0.2 jdk.editpad@11.0.2 jdk.hotspot.agent@11.0.2 jdk.httpserver@11.0.2 jdk.internal.ed@11.0.2 jdk.internal.jvmstat@11.0.2 jdk.internal.le@11.0.2 jdk.internal.opt@11.0.2 jdk.internal.vm.ci@11.0.2 jdk.internal.vm.compiler@11.0.2 jdk.internal.vm.compiler.management@11.0.2 jdk.jartool@11.0.2 jdk.javadoc@11.0.2 jdk.jcmd@11.0.2 jdk.jconsole@11.0.2 jdk.jdeps@11.0.2 jdk.jdi@11.0.2 jdk.jdwp.agent@11.0.2 jdk.jfr@11.0.2 jdk.jlink@11.0.2 jdk.jshell@11.0.2 jdk.jsobject@11.0.2 jdk.jstatd@11.0.2 jdk.localedata@11.0.2 jdk.management@11.0.2 jdk.management.agent@11.0.2 jdk.management.jfr@11.0.2 jdk.naming.dns@11.0.2 jdk.naming.rmi@11.0.2 jdk.net@11.0.2 jdk.pack@11.0.2 jdk.rmic@11.0.2 jdk.scripting.nashorn@11.0.2 jdk.scripting.nashorn.shell@11.0.2 jdk.sctp@11.0.2 jdk.security.auth@11.0.2 jdk.security.jgss@11.0.2 jdk.unsupported@11.0.2 jdk.unsupported.desktop@11.0.2 jdk.xml.dom@11.0.2 jdk.zipfs@11.0.2
4.2 –describe-module option
Java tool command adds –describe-module option to print out the detail of the module. In the following command, it will print out the detail about the java.sql module.
java –describe-module java.sql
C:\MaryZheng\DevTools\Java\JDK11\bin>java --describe-module java.sql java.sql@11.0.2 exports java.sql exports javax.sql requires java.base mandated requires java.xml transitive requires java.logging transitive requires java.transaction.xa transitive uses java.sql.Driver
In the following command, it prints out the module details of money-api-1.1.jar.
jar –file=C:\repo\javax\money\money-api\1.1\money-api-1.1.jar –describe-module
C:\MaryZheng\sts_4_10_ws\jdk9-demo>jar --file=C:\repo\javax\money\money-api\1.1\money-api-1.1.jar --describe-module java.money jar:file:///C:/repo/javax/money/money-api/1.1/money-api-1.1.jar/!module-info.class exports javax.money exports javax.money.convert exports javax.money.format exports javax.money.spi requires java.base requires java.logging uses javax.money.convert.ExchangeRateProvider uses javax.money.spi.CurrencyProviderSpi uses javax.money.spi.MonetaryAmountFactoryProviderSpi uses javax.money.spi.MonetaryAmountFormatProviderSpi uses javax.money.spi.MonetaryAmountsSingletonQuerySpi uses javax.money.spi.MonetaryAmountsSingletonSpi uses javax.money.spi.MonetaryConversionsSingletonSpi uses javax.money.spi.MonetaryCurrenciesSingletonSpi uses javax.money.spi.MonetaryFormatsSingletonSpi uses javax.money.spi.MonetaryRoundingsSingletonSpi uses javax.money.spi.RoundingProviderSpi uses javax.money.spi.ServiceProvider
Note: the module name: java.money is different from jar name: money-api.
The following command prints out the moneta details.
jar –file=C:\repo\org\javamoney\moneta\1.1\moneta-1.1.jar –describe-module
C:\MaryZheng\sts_4_10_ws\jdk9-demo>jar --file=C:\repo\org\javamoney\moneta\1.1\moneta-1.1.jar --describe-module No module descriptor found. Derived automatic module. moneta@1.1 automatic requires java.base mandated provides javax.money.convert.ExchangeRateProvider with org.javamoney.moneta.internal.convert.ECBCurrentRateProvider org.javamoney.moneta.internal.convert.ECBHistoric90RateProvider org.javamoney.moneta.internal.convert.ECBHistoricRateProvider org.javamoney.moneta.internal.convert.IMFHistoricRateProvider org.javamoney.moneta.internal.convert.IMFRateProvider org.javamoney.moneta.internal.convert.IdentityRateProvider provides javax.money.spi.CurrencyProviderSpi with org.javamoney.moneta.internal.ConfigurableCurrencyUnitProvider org.javamoney.moneta.internal.JDKCurrencyProvider provides javax.money.spi.MonetaryAmountFactoryProviderSpi with org.javamoney.moneta.internal.FastMoneyAmountFactoryProvider org.javamoney.moneta.internal.MoneyAmountFactoryProvider org.javamoney.moneta.internal.RoundedMoneyAmountFactoryProvider provides javax.money.spi.MonetaryAmountFormatProviderSpi with org.javamoney.moneta.internal.format.DefaultAmountFormatProviderSpi provides javax.money.spi.MonetaryAmountsSingletonQuerySpi with org.javamoney.moneta.internal.DefaultMonetaryAmountsSingletonQuerySpi provides javax.money.spi.MonetaryAmountsSingletonSpi with org.javamoney.moneta.internal.DefaultMonetaryAmountsSingletonSpi provides javax.money.spi.MonetaryConversionsSingletonSpi with org.javamoney.moneta.internal.convert.DefaultMonetaryConversionsSingletonSpi provides javax.money.spi.MonetaryCurrenciesSingletonSpi with org.javamoney.moneta.internal.DefaultMonetaryCurrenciesSingletonSpi provides javax.money.spi.RoundingProviderSpi with org.javamoney.moneta.internal.DefaultRoundingProvider provides javax.money.spi.ServiceProvider with org.javamoney.moneta.internal.PriorityAwareServiceProvider provides org.javamoney.moneta.spi.LoaderService with org.javamoney.moneta.internal.loader.DefaultLoaderService contains org.javamoney.moneta contains org.javamoney.moneta.convert contains org.javamoney.moneta.format contains org.javamoney.moneta.function contains org.javamoney.moneta.internal contains org.javamoney.moneta.internal.convert contains org.javamoney.moneta.internal.format contains org.javamoney.moneta.internal.loader contains org.javamoney.moneta.spi
Note: this moneta.jar is not created as a Java 9 module, so it is an automatic module with the name as the jar file name.
5. Create a Simple Java 9 Module
5.1 Pom.xml
In this step, I will create a pom.xml file which includes two dependencies.
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>jcg.zheng.demo</groupId> <artifactId>jdk9-demo</artifactId> <version>0.0.1-SNAPSHOT</version> <build> <sourceDirectory>src</sourceDirectory> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <release>11</release> <showWarnings>true</showWarnings> <showDeprecation>true</showDeprecation> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>javax.money</groupId> <artifactId>money-api</artifactId> <version>1.1</version> </dependency> <dependency> <groupId>org.javamoney</groupId> <artifactId>moneta</artifactId> <version>1.1</version> </dependency> </dependencies> </project>
5.2 module-info.java
In this step, I will create a module-info.java file which requires three modules and exports one package.
module-info.java
module module1 { requires java.base; requires java.money; requires moneta; exports org.jcg.zheng.demo; }
5.3 MoneyDemo
In this step, I will create a MoneyDemo
class which calculates money for US currency. Please note all the money objects are immutable.
MoneyDemo.java
package org.jcg.zheng.demo; import java.util.Locale; import javax.money.Monetary; import javax.money.MonetaryAmount; import javax.money.format.AmountFormatQueryBuilder; import javax.money.format.MonetaryAmountFormat; import javax.money.format.MonetaryFormats; import org.javamoney.moneta.format.CurrencyStyle; public class MoneyDemo { public static void main(String[] args) { MonetaryAmount oneDollar = Monetary.getDefaultAmountFactory().setCurrency("USD").setNumber(1).create(); MonetaryAmount twoDollar = Monetary.getDefaultAmountFactory().setCurrency("USD").setNumber(2).create(); System.out.println("Curreny=" + oneDollar.getCurrency().toString()); MonetaryAmount threeDollar = oneDollar.add(twoDollar); System.out.println(oneDollar.toString()); System.out.println(twoDollar.toString()); System.out.println(threeDollar.toString()); MonetaryAmountFormat customFormat = MonetaryFormats.getAmountFormat(AmountFormatQueryBuilder.of(Locale.US) .set(CurrencyStyle.NAME).set("pattern", "00.00 US Dollar").build()); System.out.println(customFormat.format(oneDollar)); System.out.println(customFormat.format(twoDollar)); System.out.println(customFormat.format(threeDollar)); } }
5.4 Demo
This MoneyDemo
depends on two third-party modules, so make sure that the depended modules are added in the Modulepath. See Figure 3 for Modulepath details.
Click the “Module Dependencies” tab to see the module’s dependencies. See Figure 4.
Run the MoneyDemo
class as a Java application and capture the output here.
MoneyDemo output
May 31, 2021 10:27:53 AM org.javamoney.moneta.DefaultMonetaryContextFactory createMonetaryContextNonNullConfig INFO: Using custom MathContext: precision=256, roundingMode=HALF_EVEN Curreny=USD USD 1 USD 2 USD 3 01.00 US Dollar 02.00 US Dollar 03.00 US Dollar
Note: oneDollar
, twoDollar
, and threeDollar
are immutable.
6. Summary
In this example, I explained the key features (along with example link) introduced at Java 8 and Java 9. I also demonstrated the following new features at Java 9:
- STS IDE build path is updated to include module dependencies.
- Java tool command is updated to describe modules.
- How to include modules in the Java 9 module project.
- How to export Java packages inside a module.
Please note that Java 9 is not a LTS version and the current LTS version is 11.
7. More articles
- Java 8 Features Tutorial
- Download and Install Java Development Kit (JDK) 8
- Download and Install Java Development Kit (JDK) 11
- Download and Install Java Development Kit (JDK) 13
8. Download the Source Code
You can download the full source code of this example here: Java 8 vs Java 9