Skip to content

Latest commit

 

History

History
144 lines (101 loc) · 4.24 KB

File metadata and controls

144 lines (101 loc) · 4.24 KB
title Overview of New Features in Java 12 & 13
category Java
tag
New Features in Java

Java 12

String Enhancements

Java 12 introduced two new string processing methods, as shown below.

The indent() method can be used to indent a string.

String text = "Java";
// Indent by 4 spaces
text = text.indent(4);
System.out.println(text);
text = text.indent(-10);
System.out.println(text);

Output:

     Java
Java

The transform() method can be used to transform a specified string.

String result = "foo".transform(input -> input + " bar");
System.out.println(result); // foo bar

Files Enhancements (File Comparison)

Java 12 added the following method to compare two files:

public static long mismatch(Path path, Path path2) throws IOException

The mismatch() method is used to compare two files and returns the position of the first mismatched character. If the files are the same, it returns -1L.

Code example (when the contents of the two files are the same):

Path filePath1 = Files.createTempFile("file1", ".txt");
Path filePath2 = Files.createTempFile("file2", ".txt");
Files.writeString(filePath1, "Java 12 Article");
Files.writeString(filePath2, "Java 12 Article");

long mismatch = Files.mismatch(filePath1, filePath2);
assertEquals(-1, mismatch);

Code example (when the contents of the two files are different):

Path filePath3 = Files.createTempFile("file3", ".txt");
Path filePath4 = Files.createTempFile("file4", ".txt");
Files.writeString(filePath3, "Java 12 Article");
Files.writeString(filePath4, "Java 12 Tutorial");

long mismatch = Files.mismatch(filePath3, filePath4);
assertEquals(8, mismatch);

Number Formatting Utility Class

NumberFormat has added support for formatting complex numbers.

NumberFormat fmt = NumberFormat.getCompactNumberInstance(Locale.US, NumberFormat.Style.SHORT);
String result = fmt.format(1000);
System.out.println(result);

Output:

1K

Shenandoah GC

Shenandoah GC, developed under the leadership of Redhat, aims for 99.9% of pauses to be less than 10ms, with pauses being independent of heap size.

Compared to the open-source ZGC introduced in Java 11 (which requires an upgrade to JDK 11 to use), Shenandoah GC has a stable version for JDK 8, making it more feasible in today's market where Java 8 holds a significant share.

G1 Collector Optimizations

Java 12 brought two updates to the default garbage collector, G1:

  • Abortable Mixed Collections: Implemented in JEP 344, this allows the G1 garbage collector to abort the garbage collection process to meet user-defined pause time goals by splitting the regions to be reclaimed (mixed collection set) into mandatory and optional parts. G1 can abort the reclamation of optional parts to meet pause time goals.
  • Promptly Return Unused Allocated Memory: Implemented in JEP 346, this enhances G1 GC to automatically return Java heap memory to the operating system when it is idle.

Preview Features

As preview features, they require the --enable-preview option when compiling with javac and running with java.

Enhanced Switch

The traditional switch syntax has a problem of easily forgetting to write break, and from the perspective of code cleanliness, multiple breaks are essentially a form of repetition.

Java 12 enhances the switch expression, using a lambda-like syntax for the execution block after a successful condition match, eliminating the need for multiple breaks.

switch (day) {
    case MONDAY, FRIDAY, SUNDAY -> System.out.println(6);
    case TUESDAY                -> System.out.println(7);
    case THURSDAY, SATURDAY     -> System.out.println(8);
    case WEDNESDAY              -> System.out.println(9);
}

instanceof Pattern Matching

instanceof is primarily used to detect the specific type of an object before type casting.

In previous versions, we needed to explicitly cast the object.

Object obj = "I am a string";
if(obj instanceof String){
   String str = (String) obj;
   System.out.println(str);
}

In the new version, instanceof can perform the type check and cast in one step.

Object obj = "I am a string";
if(obj instanceof String str){
  System.out.println(str);
}

Java