Java 12 major language and API improvements
Java ⑫ has been released in March 2019.
The following content is summarizing the most important changes to the Java API and the Java language introduced with Java ⑫.
For a more complete overview follow the links in the following sections referring to the official Oracle release documents.
API improvements
The following describes selected improvements of the Java API. See the Java 12 release notes.
String
var multiline = """
Java
12
rocks!
""";
assertEquals("""
Java
12
rocks!
""", multiline.indent(3));
With String::indent
we can easily indent any string. With earlier Java versions we would have to use 3rd-party libraries such as Apache commons-lang or implement yourself..
var multiline = """
Java
12
rocks!
""";
assertEquals("""
java
12
rocks!
""", multiline.transform(String::toLowerCase));
With String::transform
we can easily transform any string.
Files
Path tmpdir = Path.of(System.getProperty("java.io.tmpdir"));
Path path11 = Files.writeString(Files.createTempFile(tmpdir, "version", ".properties"), "Java 11 rocks");
Path path12 = Files.writeString(Files.createTempFile(tmpdir, "version", ".properties"), "Java 12 rocks");
long position = Files.mismatch(path11, path12);
assertEquals(6, position);
With Files.mismatch
we can detect the first mismatching character while comparing two files.
NumberFormat
NumberFormat format = NumberFormat
.getCompactNumberInstance(Locale.US, NumberFormat.Style.SHORT);
String result = format.format(1_000); // "1K"
Collectors
double avgIncome = Stream.of(2000.0, 3000.0, 4000.0)
.collect(Collectors
.teeing(
Collectors.summingDouble(v -> v),
Collectors.counting(),
(sum, count) -> sum / count)
);
assertEquals(3000.0, avgIncome);
}
}
With Collectors.teeing
we can have two collectors process a stream.
The results of both collectors are then processed by a merge function.
JVM improvements
The following describes selected improvements of the Java JVM. See the Java 12 release notes.
With JEP-341, now CDS archives are built during the JDK build. CDS is already on by default as of Java ⑪.