
Welcome to our course on Java 14! In this course, you will learn about the latest features and enhancements in the Java programming language. Java 14 was released in March 2020, and it includes a number of exciting new features that make it easier and more efficient to write code.
In this course, we will cover the most important new features in Java 14, including records, pattern matching, and text blocks. We will also show you how to use these new features to write more concise and expressive code, and we will provide examples of how to apply them to common programming tasks.
Whether you are a beginner looking to learn Java 14 from scratch, or an experienced Java developer looking to upgrade your skills, this course has something for you. By the end of this course, you will have a solid understanding of Java 14 and be well-prepared to use it to build your own applications. Let’s get started!
Records
A new type of class that allows you to concisely and easily define data objects. Records have a compact syntax and automatically provide getter methods, toString, equals, and hashCode implementations, among other things. Look at below example
// Define a record that represents a person
record Person(String firstName, String lastName, int age) {}
// Create an instance of the Person record
Person p = new Person("John", "Doe", 25);
// Access the record fields using the automatically generated getters
System.out.println(p.firstName()); // prints "John"
System.out.println(p.lastName()); // prints "Doe"
System.out.println(p.age()); // prints 25
// The record also provides an automatically generated toString method
System.out.println(p); // prints "Person[firstName=John, lastName=Doe, age=25]"
Pattern matching for instanceof
A new syntax that allows you to match the type of an object against a pattern, rather than just checking if it is an instance of a particular type. This makes it easier to write code that handles different types of objects in a more elegant and readable way. Look at below example
Object o = "Hello, world!";
// Check if o is a String, and if so, print its length
if (o instanceof String s) {
System.out.println(s.length()); // prints "13"
}
Text blocks
A new syntax for representing multi-line string literals in your code. Text blocks make it easier to write strings that span multiple lines and include special characters, such as newlines, without having to use escape sequences. Look at below example
// Define a multi-line string using a text block
String s = """
Lorem ipsum dolor sit amet,
consectetur adipiscing elit.
""";
// Print the string, including the newline characters
System.out.println(s);
// prints:
// "
// Lorem ipsum dolor sit amet,
// consectetur adipiscing elit.
// "
Switch expressions
An update to the switch statement that allows you to use the “arrow” syntax (->
) to specify a value that is returned from a switch case. This makes it possible to use switch statements as expressions, rather than just statements. Look at below example
public String getDayOfWeek(int day) {
return switch (day) {
case 1 -> "Monday";
case 2 -> "Tuesday";
case 3 -> "Wednesday";
case 4 -> "Thursday";
case 5 -> "Friday";
case 6 -> "Saturday";
case 7 -> "Sunday";
default -> throw new IllegalArgumentException("Invalid day: " + day);
};
}
Foreign-memory access API
A new API that allows Java programs to safely and efficiently access foreign memory outside of the Java heap. This can be useful for working with large data sets or for interfacing with native libraries.
Here is an example of how to use the foreign-memory access API to allocate a memory segment and read a value from it:
// Allocate a memory segment of 100 bytes MemorySegment segment = MemorySegment.allocateNative(100); // Get the address of the first byte in the segment MemoryAddress address = segment.address(); // Read an int value from the segment at the given address int value = address.getInt();
In this example, we first allocate a memory segment of 100 bytes using the MemorySegment.allocateNative()
method. We then obtain the address of the first byte in the segment using the MemorySegment.address()
method, and use the MemoryAddress.getInt()
method to read an int
value from the memory segment at that address.
The foreign-memory access API also allows you to write values to a memory segment, as well as perform other operations such as copying, slicing, and releasing memory segments. For more information and examples, you can refer to the Java 14 documentation.
New garbage collectors
Java 14 includes two new garbage collectors, Epsilon and ZGC, that can be used to improve the performance and scalability of Java applications. Epsilon is a no-op garbage collector that can be used to measure the overhead of garbage collection, while ZGC is a low-pause, scalable collector that is designed to handle large heaps.
To use the Epsilon garbage collector, you can specify the -XX:+UseEpsilon
flag when starting your Java application. For example:
java -XX:+UseZGC MyApplication
When ZGC is enabled, it will automatically adjust its behavior based on the size of the Java heap and the workload of your application. It will try to minimize pause times and provide consistent performance, even when working with very large heaps. Note that these flags are just examples, and there are many other options and settings that you can use to fine-tune the behavior of the Epsilon and ZGC garbage collectors. For more information and examples, you can refer to the official Java documentation. https://docs.oracle.com/en/java/javase/14/docs/api/index.html