Top features of Java 9
Java 9 introduces several new features and enhancements that make it easier to write efficient, maintainable, and scalable code.
- Modularization: Java 9 introduced the Java Platform Module System (JPMS), which provides a modular architecture for Java applications. With JPMS, developers can create modular applications that can be easily managed and updated.
Modularization allows developers to break up their code into small, self-contained modules that can be easily managed and updated. For example, a developer could create a module for the UI layer, another for the business logic, and another for the data access layer. This makes it easier to maintain and update the application, as changes can be made to individual modules without affecting the rest of the application.
2. JShell: JShell is a new interactive Java shell that allows developers to test Java code snippets and evaluate them immediately without having to create a full Java class or application.
JShell provides a way for developers to quickly test out ideas and experiment with Java code. For example, a developer could use JShell to test out a new algorithm or explore the behavior of a particular Java class or method.
2.1 Open a terminal or command prompt and type jshell
to launch JShell.
2.2 Type int x = 5;
to declare an integer variable called x
with a value of 5.
2.3 Type x + 3;
to perform an arithmetic operation and see the result, which should be 8.
2.4 Type import java.util.*;
to import the java.util
package.
2.5 Type List<String> myList = Arrays.asList("apple", "banana", "orange");
to declare a List
of String
elements called myList
with three fruits.
2.6 Type myList.stream().sorted().forEach(System.out::println);
to sort the elements of myList
alphabetically and print them to the console.
2.7 You should see the output apple banana orange
.
In this example, we used JShell to experiment with Java code in an interactive environment. We declared a variable, performed an arithmetic operation, imported a package, created a List
of elements, and sorted and printed the elements.
JShell is a powerful tool that can be used for rapid prototyping, debugging, and learning Java. It allows you to test code snippets and see the results immediately, without the need to create a full Java application or file. You can also save your JShell sessions and reuse them later or share them with others.
3. Performance improvements: Java 9 includes several performance improvements, including better memory management and faster startup times.
Java 9 includes several performance improvements that make Java applications run faster and use less memory. For example, the Garbage Collector has been improved to be more efficient, which reduces the amount of time that an application spends waiting for garbage collection to occur.
One example of a performance improvement in Java 9 is the introduction of the “Compact Strings” feature. Prior to Java 9, all strings in Java were represented as an array of characters (i.e., UTF-16 encoding). However, for strings that only contained characters from the ASCII character set, this representation was wasteful, as each character was still stored using two bytes.
In Java 9, the “Compact Strings” feature was introduced, which represents strings that only contain characters from the ASCII character set using a single byte per character. This results in significant memory savings for strings that meet this criterion, which can lead to improved performance and reduced garbage collection overhead.
For example, suppose a Java application processes a large amount of text data that consists mainly of ASCII characters. In this case, the use of Compact Strings in Java 9 can significantly reduce the amount of memory used by the application, which can improve its overall performance.
4. Stream API enhancements: Java 9 includes enhancements to the Stream API that make it easier to work with streams of data.
The Stream API in Java 9 includes several enhancements that make it easier to work with streams of data. For example, the Stream API now includes methods for iterating over a stream in reverse order, and for taking the first N elements of a stream.
5. Multi-release JARs: Java 9 introduced the ability to create JAR files that contain different versions of the same class for different Java releases. This feature makes it easier for developers to write code that works across multiple Java versions.
Multi-release JARs allow developers to create JAR files that contain different versions of the same class for different Java releases. For example, a developer could create a JAR file that contains different versions of a class for Java 8 and Java 9, which would allow the same code to run on both Java 8 and Java 9.
Here’s an example of how you can create a Multi-release JAR:
- Create a Java project and add two versions of the same class, one for Java 8 and one for Java 9 or higher. In this example, we’ll use a simple
HelloWorld
class:
// Java 8 version of HelloWorld.java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, Java 8!");
}
}
// Java 9 version of HelloWorld.java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, Java 9 or higher!");
}
}
2. Create a directory named META-INF/versions
in your project's root directory.
3. Inside the versions
directory, create subdirectories with the names of the Java versions you want to support. For example, if you want to support Java 8 and Java 9 or higher, create subdirectories named 8
and 9
, respectively.
4. Inside each subdirectory, add a copy of the class file that corresponds to that Java version. For example:
project-root/
src/
HelloWorld.java
META-INF/
versions/
8/
HelloWorld.class (compiled from the Java 8 version of HelloWorld.java)
9/
HelloWorld.class (compiled from the Java 9 version of HelloWorld.java)
5. Build your project and create a JAR file that includes all the class files, as well as the META-INF
directory. For example, using the jar
command:
jar cvf HelloWorld.jar -C <project-root>/ .
6. Run the JAR file on different versions of Java to see which version of the HelloWorld
class is used. For example:
java -jar HelloWorld.jar // uses the Java 9 or higher version of the class
java -jar -Djava.version=1.8 HelloWorld.jar // uses the Java 8 version of the class
When you run the JAR file without specifying a Java version, the Java runtime will automatically select the version of the class that corresponds to its own version. If you specify a Java version using the -Djava.version
flag, the Java runtime will use the version of the class that corresponds to that version.
Note that in order to use Multi-release JARs, you need to be using Java 9 or higher. If you’re using an older version of Java, the JAR file will behave as if it contains only one version of each class or resource.
6. Reactive Streams: Java 9 includes a new API for Reactive Streams, which is a programming model for asynchronous, non-blocking, and backpressure-aware communication between components.
Reactive Streams is a programming model for asynchronous, non-blocking, and backpressure-aware communication between components. For example, a developer could use Reactive Streams to build a high-performance, scalable, and responsive web application.
7. HTTP/2 Client: Java 9 includes a new HTTP/2 client API, which makes it easier to write HTTP/2 client applications.
The new HTTP/2 client API in Java 9 makes it easier to write HTTP/2 client applications. For example, a developer could use the HTTP/2 client API to build a web application that uses HTTP/2 to communicate with the server.
8. Unicode enhancements: Java 9 includes enhancements to the Unicode support, including new methods for working with Unicode code points.
Java 9 includes enhancements to the Unicode support, which makes it easier to work with Unicode characters and strings. For example, Java 9 includes new methods for working with Unicode code points, which makes it easier to manipulate Unicode strings.
9. Process API enhancements: Java 9 includes enhancements to the Process API that make it easier to manage and monitor operating system processes from within Java applications.
Java 9 includes enhancements to the Process API, which makes it easier to manage and monitor operating system processes from within Java applications. For example, a developer could use the Process API enhancements to start and stop external processes and monitor the resource usage of those processes.
Overall, these new features and enhancements in Java 9 provide developers with powerful tools to build high-performance, scalable, and modular applications.