Introduce
Today I would like to introduce the outstanding new features from java 11 onwards, helping you to be more confident to upgrade to java 17.
Text Blocks (Java 15)
Text Blocks is a feature developed from java 13, but it was not until java 15 that it was officially put into use. Text Blocks make java string usage easier
Java old version
1 2 3 4 5 | String demo = "Hoàng Phúc International" + "n" + "kafkan" + "kafkan"; |
Java new version
1 2 3 4 5 6 7 | String demo = """ Hoàng Phúc International kafka kafka """; |
To use variable values in Text Blocks you can use the String.format function, for example:
1 2 3 4 | String demo = """ Some parameter: %s """.formatted("HPI"); |
Records
record is a new type in java 15 that helps you to quickly initialize objects.
Let’s make an example to see the convenience compared to using Class
Using Class, initialization is relatively long and cumbersome =))
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | public class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; } } |
Using Records, the syntax is very compact and without the need for a 3rd party library like lombok, the name and age variables will be stored by the record.
1 2 3 4 5 6 | public record Person(String name, int age) { } Person p = new Person("hpi",32); p.name(); |
Sealed Classes
Classes when declared in java will not control inheritance, any Class can inherit your Class, to overcome this problem Sealed Classes were introduced in java 15.
Sealed Classes control inheritance, classes can only inherit Sealed Classes when Sealed Classes grant inheritance
1 2 3 4 5 | public abstract sealed class Person permits ClassA, ClassB { } |
When using permits ClassA, ClassB
you are granting permissions to ClassA, ClassB is allowed to inherit from classes Person
any other class when inheriting will receive an error message.
Pattern Matching Type Checks
From java version 14 and above, checking Type will become syntactically simpler, this change usually doesn’t make a difference in performance but makes your code a lot more transparent.
1 2 3 4 5 6 7 8 9 10 | /// OLD if (person instanceof Employee) { Employee employee = (Employee) person; } /// NEW if (person instanceof Employee employee) { Date hireDate = employee.getHireDate(); } |
Vector API
The Vector API was first developed in java 16. The idea of this API is to provide a means of vector computation that will eventually be able to perform more optimally than traditional scalar computation methods.
1 2 3 4 5 6 7 8 9 | int[] a = {1, 2, 3, 4}; int[] b = {5, 6, 7, 8}; var c = new int[a.length]; for (int i = 0; i < a.length; i++) { c[i] = a[i] * b[i]; } |
This calculation example, for an array of length 4, will execute in 4 cycles. Now, let’s look at the Vector-based calculation:
1 2 3 4 5 6 7 8 | int[] a = {1, 2, 3, 4}; int[] b = {5, 6, 7, 8}; var vectorA = IntVector.fromArray(IntVector.SPECIES_128, a, 0); var vectorB = IntVector.fromArray(IntVector.SPECIES_128, b, 0); var vectorC = vectorA.mul(vectorB); vectorC.intoArray(c, 0); |
Vector helps you to be more optimal when you work with arrays.