Make writing a habit together! This is the 8th day of my participation in the “Gold Digging Day New Plan · April More Text Challenge”. Click here for more details.

Java8 was released on March 18, 2014. As of April 6, 2022, the current latest release is Java18. Versions 17, 11, and 8 are the long Term Support (LTS) versions currently supported. This article takes you through the features of each version of Java from 8 onwards. Sit back and go! Want to have a crush on an article, click here to interview the kill | please discuss Java8-18 of the new features introduced (2)

New features in Java 12

Compact Number Formatting

Java12 brings a new number formatter, CompactNumberFormat. It is designed to represent a number in shorter form, based on the pattern provided by a given region.

We can get an instance of it using the getCompactNumberInstance method in the NumberFormat class:

public static NumberFormat getCompactNumberInstance(Locale locale, NumberFormat.Style formatStyle)
Copy the code

As mentioned earlier, the locale parameter is responsible for providing the appropriate format pattern. The format style can be SHORT or LONG. To better understand the format style, let’s consider the 1000 for the United States region. The SHORT style will be formatted as “10K” and the LONG style will be formatted as “10000”.

Now let’s look at an example, “number of likes in an article”, and then compress it using two different styles:

@Test public void givenNumber_thenCompactValues() { NumberFormat likesShort = NumberFormat.getCompactNumberInstance(new Locale("en", "US"), NumberFormat.Style.SHORT); likesShort.setMaximumFractionDigits(2); AssertEquals (" 2.59 K, "likesShort format (2592)); NumberFormat likesLong = NumberFormat.getCompactNumberInstance(new Locale("en", "US"), NumberFormat.Style.LONG); likesLong.setMaximumFractionDigits(2); AssertEquals (" 2.59 thousand ", likesLong format (2592)); }Copy the code

pattern matching for instanceof

Another preview feature introduced in Java 12 is Instanceof pattern matching.

In previous Versions of Java, when using the if statement and instanceof, we had to explicitly cast an object to access its properties:

Object obj = "Hello World!" ; if (obj instanceof String) { String s = (String) obj; int length = s.length(); }Copy the code

With Java 12, we can declare the new typed variable directly in a statement:

if (obj instanceof String s) {
    int length = s.length();
}
Copy the code

String Class New Methods

Java 12 provides two new methods in the String class.

First, the string indentation adjusts the indentation of each line according to the integer argument. If the argument is greater than zero, a new space is inserted at the beginning of each line. On the other hand, if the argument is less than zero, whitespace is removed from each line of the request. If a given line does not contain enough whitespace, all leading whitespace characters are removed.

Now, let’s look at a basic example. First, we indent the text to four Spaces, then remove the entire indentation:

String text = "Hello world! \nThis is Java."; text = text.indent(4); System.out.println(text); text = text.indent(-10); System.out.println(text);Copy the code

The output is as follows:

    Hello world!
    This is Java.
​
Hello world!
This is Java.
Copy the code

Note that even if we pass value-10(beyond the indent count), only Spaces are affected and other characters remain intact.

The second new method is transformation, which takes a single-argument function as an argument to be applied to a string.

To take a simple example, let’s use the transform method to restore a string:

@Test
public void givenString_thenRevertValue() {
    String text = "Baeldung";
    String transformed = text.transform(value ->
      new StringBuilder(value).reverse().toString()
    );
​
    assertEquals("gnudleaB", transformed);
}
Copy the code

There are other new features, including but not limited to:

  • File::mismatch in the nio.file.Files utility class
  • Teeing Collector
  • Preview Changes
  • JVM Changes

To continue, the following will continue to talk about the new features of each version, stay tuned!