Table of Contents
12.1. Changes in Switch expressions:
Switch expressions will now be used as a statement as well as expressions. This makes code simplification and pattern matching possible for the switch.
- In earlier versions, missing break statements resulted in default fall through which was error-prone.
- The default case is mandatory in the switch expressions.
Now, the new Arrow syntax for switch introduced as
case X -> {}
Denotes that only if the label matches, statements on the right side of the arrow will be executed.
The Switch expressions of Java 11 and Java 12 can be compared as follows:
Java 11
- Java
The mealNumber is : 2
Java 12
- Java
Output
The mealNumber is : 2
12.2. Shenandoah (A new and improved Garbage Collector)
This is an experimental feature and introduces a new garbage collection (GC) algorithm, Shenandoah.
It offers low pause time by concurrent execution of evacuation work with running Java threads. With this, pause times are independent of the heap size. For example, a 5MB heap will have the same pause time as that of a 10GB one.
12.3. JVM constants API
This API helps those programs that manipulate classes and methods. These programs need to model byte code instructions and handle loadable constants. Constants of the type String or Integer work fine.
However, it becomes tricky with Constant type as Class. Loading classes can fail if Class is inaccessible or doesn’t exist. With the new API in place, interfaces such as ClassDesc, MethodTypeDesc, MethodHandleDesc, and
DynamicConstantDesc, handle constant values symbolically thereby eliminating complexity.
12.4. Abortable mixed collections for G1:
The default garbage collector, Garbage First (G1), uses an analysis engine to determine the collection set and once the collection starts, all live objects should be collected without stopping. This results in exceeding the target pause time. To address this issue, G1 collection sets are made abortable by breaking the set into optional and mandatory parts. By prioritizing the mandatory set, the pause time target can be achieved often.
Promptly return unused committed memory from G1:
With this improved feature, when G1 is idle, the garbage collector automatically returns unused heap memory to the operating system. This is done by concurrent checks of Java heap by G1.
12.5 Files.mismatch() method:
This new method compares two files.
Method Signature
public static long mismatch(Path path1, Path path2) throws IOException
Returns: -1L if no mismatch else Position of the first mismatch.
It returns the position of the mismatch in either of the two cases.
- Case 1: if the size of the files does not match. Here, the size of the smaller file is returned.
- Case 2: if the bytes does not match. Here, the first mismatching byte is returned.
Example:
- Java
Output
Mismatch position in file1 and file2 is : -1
Mismatch position in file3 and file4 is : 10
12.6 Compact Number Formatting:
It is the formatting applied to general-purpose numbers e.g. decimal, currency, percentage to make them compact due to space constraint. In the below example, 1000 will be formatted as ‘1K’ in a short style and ‘1 thousand’ in a long style.
- Java
Output
100
1 thousand
10 thousand
100
1K
10K
10.7 Teeing Collectors in Stream API:
Collectors.teeing() is the new helper function that helps in performing two steps function into a single step. This results in less verbose code.
Method Signature
public static Collector teeing (Collector downstream1, Collector downstream2, BiFunction merger);
Here, we are performing two different stream operations on two different collectors and the result is merged using the supplied BiFunction.
Example:
- Java
Output
4.0
12.8 Java String New Methods:
Java 12 introduced the following new methods in the String class:
i) indent(int n): It adjusts the indentation of each line of the given string based on the argument passed.
Based on the value of n passed, we can have the following cases :
- If n > 0, spaces are inserted at beginning of each line
- If n < 0, spaces are removed at the beginning of each line
- If n < 0 and n < available white spaces, all leading spaces are removed
- If n = 0, line remains unchanged
String str = "**********\n Welcome\n Good Morning\n**********";
System.out.println(str.indent(0));
System.out.println(str.indent(3));
Output
**********
Welcome
Good Morning
**********
**********
Welcome
Good Morning
**********
**********
Welcome
Good Morning
**********
ii) transform(Function<? super String,? extends R> f): It is used to call a function expecting a string argument and producing result R.
String s = "Java,Python,Angular";
List result = s.transform(s1 -> {return Arrays.asList(s1.split(","));});
System.out.println(result);
Output
[Java, Python, Angular]
iii) Optional<String> describeConstable(): This method will return an Optional object containing a descriptor for the String instance.
String message = "Welcome!";
Optional<String> opOfMessage = message.describeConstable();
System.out.println(opOfMessage);
Output
Optional[Welcome!]
iv) String resolveConstantDesc(MethodHandles.Lookup lookup): This method will return a String object which is the descriptor for the invoking String instance.
String message = "Welcome!";
String constantDesc = message.resolveConstantDesc(MethodHandles.lookup());
System.out.println(constantDesc);
Output
Welcome!
Though Java 12 is yet to gain popularity as compared to Java 8, still the addition of new features more frequently is making Java comparable with better features of other languages thus maintaining its popularity in the market.