What are the new features of java-12?

On March 2019, Oracle released a new version of Java as Java-12. Following are the new features of java-12

String Class New Methods:

Following new methods are added in the String class as a part of the Java-12 update.

  1. indent(int n)
  • Adjusts the indentation of each line of this string based on the value of n, and normalizes line termination characters. 
  • This string is conceptually separated into lines using String.lines(). Each line is adjusted as described below and then suffixed with a line feed “\n” (U+000A). The resulting lines are then concatenated and returned. 
  • If n > 0 then n spaces (U+0020) are inserted at the beginning of each line. 
  • If n < 0 then up to n white space characters are removed from the beginning of each line. If a given line does not contain sufficient white space then all leading white space characters are removed. Each white-space character is treated as a single character. In particular, the tab character “\t” (U+0009) is considered a single character; it is not expanded. 
  • If n == 0 then the line remains unchanged. However, line terminators are still normalized.
  • The following example is for the indent method.

Output: We can change the spaces respectively changing based on the value of ‘n’

  1. transform(Function<? super String, ? extends R> f)
  • This method allows the application of a function to this string. The function should expect a single String argument and produce an R result. 
  • Any exception thrown by f() will be propagated to the caller.
  • Following is the example of the transform method

Output for the above code is as follows:

  1. describeConstable()

This Returns an Optional containing the nominal descriptor for this instance, which is the instance itself.

  1. resolveConstantDesc():

This resolves this instance as a ConstantDesc, the result of which is the instance itself.

Teeing method of Collector:

teeing(Collector<? super T, ?, R1> downstream1,

                              Collector<? super T, ?, R2> downstream2,

                              BiFunction<? super R1, ? super R2, R> merger)

This Returns a Collector that is a composite of two downstream collectors. Every element passed to the resulting collector is processed by both downstream collectors, then their results are merged using the specified merge function into the final result. 

The resulting collector functions do the following: 

  • supplier: creates a result container that contains result containers obtained by calling each collector’s supplier 
  • accumulator: calls each collector’s accumulator with its result container and the input element 
  • combiner: calls each collector’s combiner with two result containers 
  • finisher: calls each collector’s finisher with its result container, then calls the supplied merger and returns its result. 

The resulting collector is Collector.Characteristics.UNORDERED if both downstream collectors are unordered and Collector.Characteristics.CONCURRENT if both downstream collectors are concurrent.

Example

Output: 20

In the above example sum of values 1 + 2 + 3 + 4 + 5 =  15 and count is 5 so the total is 15+5 = 20.

The mismatch Method for File: 

mismatch(Path path, Path path2)

Finds and returns the position of the first mismatched byte in the content of two files, or -1L if there is no mismatch. The position will be in the inclusive range of 0L up to the size (in bytes) of the smaller file. 

Two files are considered to match if they satisfy one of the following conditions: 

  • The two paths locate the same file, even if two equal paths locate a file that does not exist, or 
  • The two files are the same size, and every byte in the first file is identical to the corresponding byte in the second file. 

Otherwise, there is a mismatch between the two files and the value returned by this method is: 

  • The position of the first mismatched byte, or 
  • The size of the smaller file (in bytes) when the files are different sizes and every byte of the smaller file is identical to the corresponding byte of the larger file. 

This method may not be atomic with respect to other file system operations. This method is always reflexive (for Path f, mismatch(f,f) returns -1L). If the file system and files remain static, then this method is symmetric (for two Paths f and g, mismatch(f,g) will return the same value as mismatch(g,f)).

Following is the example of the above method:

Output if contents are matching:

Output if contents are not matching:

Compact Number Formatting

This returns a compact number format for the specified locale and format style as shown in the example below:

Output: 

100K

1M

100 thousand

1 million

Java-12 has also added previews of switch statements (where we can add comma-separated case values(available from Java-14 onwards) and Pattern Matching for instanceof where we can directly cast into the object while checking the instance (available from Java-16 onwards)

Also, see:

Related Posts