Java 14 New Features

By Arvind Rai, July 05, 2021
On this page we will discuss new features and enhancements introduced in Java 14.

Pattern Matching for instanceof Operator (JEP 361)

This is a preview feature in Java 14. The instanceof operator has been changed for pattern matching of an object that will make code more concise and robust.
Look at the below code.
if (animal instanceof Cow) {
  Cow a = (Cow) animal;
  System.out.println(a.getCowName());
} 
Now with new instanceof, we can write above code as following.
if (animal instanceof Cow a) {
  System.out.println(a.getCowName());
} 
Find the sample example.
package com.concretepage;

interface Animal {
}
class Cow implements Animal {
  String name;

  public Cow(String name) {
	this.name = name;
  }

  public String getCowName() {
	return name;
  }
}
class Horse implements Animal {
  String name;

  public Horse(String name) {
	this.name = name;
  }

  public String getHorseName() {
	return name;
  }
}
public class MyApp {
  public static void main(String[] args) {
	Animal ob = new Cow("Cow");
	getName(ob);
  }
  static String getName(Animal animal) {
	if (animal instanceof Cow a) {
	  System.out.println(a.getCowName());
	} else if (animal instanceof Horse a) {
	  System.out.println(a.getHorseName());
	}
	return null;
  }
} 

Records (JEP 359)

This is a preview feature in Java 14. The record is a new kind of type declaration like enum. It is used for "plain data carriers" classes that contain data not meant to be altered and only the most fundamental methods such as constructors and accessors. Find the sample example.
package com.concretepage;

record Rectangle(int length, int width) { }

public class MyApp {
  public static void main(String[] args) {
	Rectangle rec = new Rectangle(12, 5);
	System.out.println(rec.length()); //12
	System.out.println(rec.width()); //5
  }
} 
In the above code a record has been declared as Rectangle.
record Rectangle(int length, int width) { } 
The above one liner code is equivalent to below code.
final class Rectangle {
  final int length;
  final int width;
  public Rectangle(int length, int width) {
      this.length = length;
      this.width = width;
  }
  int length() { return length; }
  int width() { return width; }
} 
Hence we can draw the following conclusion.
1. All the members of record are final.
2. We have only accessors methods with same name as field names in a record.

Switch Expressions (JEP 361)

Switch Expressions was introduced in Java 12 as preview feature. Now this feature has been adopted as permanent in Java 14. Switch expressions are used as "case L ->" and eliminates the use of break keyword. The switch expression has been extended to produce value using yield in Java 13.
Find the sample example.
package com.concretepage;
public class MyApp {
  public static void main(String[] args) {
	int day = 2;
	String x = switch (day) {
	  case 1 -> "Monday";
	  case 2 -> "Tuesday";
	  case 3 -> "Wednesday";
	  default -> "NA";
	};
	System.out.println(x);
  }
} 
Output will be Tuesday.

Accounting Currency Format Support (JDK-8215181)

In some locales, accounting currency format supports parenthesis such as using "u-cf-account" unicode locale extension, the locale Locale.US will format to "($20.50)" instead of "-$20.50".
DecimalFormat df1 = (DecimalFormat) DecimalFormat.getCurrencyInstance(Locale.US);
System.out.println(df1.format(-20.50)); //-$20.50

Locale myLocale = new Locale.Builder().setLocale(Locale.US)
	.setExtension(Locale.UNICODE_LOCALE_EXTENSION, "cf-account").build();
DecimalFormat df2 = (DecimalFormat) NumberFormat.getCurrencyInstance(myLocale);
System.out.println(df2.format(-20.50)); //($20.50) 

Other New Features and Enhancements

1. JEP 359: A new class java.lang.Record has been introduced as preview feature. As we know that java.lang.* is imported by default in our java class. If we create a class named Record in our package such as com.cp and when we write import com.cp.* then our Record class will not be imported. We need to import it explicitly as following.
import com.cp.Record


2. JDK-8164993: The following methods specification has been changed to specify that an IllegalArgumentException will be thrown if the buffer parameters are read-only.
DatagramChannel.receive()
FileChannel.read(ByteBuffer,long)
ReadableByteChannel.read()
ScatteringByteChannel.read() 
3. JEP 364 and 365: The Z Garbage Collector (ZGC) is now available as an experimental feature on Windows as well as macOS. To enable it, we need to use JVM flags as following.
- XX:+UnlockExperimentalVMOptions -XX:+UseZGC 
4. JDK-8224666: Parallel GC has adopted the same task management mechanism for scheduling parallel tasks as other collectors. This will result in significant performance improvements.

5. JEP 345: The G1 garbage collector now tries to allocate and keep objects on the same NUMA node in the young generation across garbage collections.

6. JEP 349: JDK Flight Recorder (JFR) now supports continuous monitoring of a Java application by allowing events to be consumed dynamically using a new API located in the jdk.jfr.consumer package.

7. JDK-8233228: Weak Named Curves in TLS, CertPath, and Signed JAR are disabled by default.

8. JDK-8231507: The Apache Santuario library has been upgraded to version 2.1.4. A new property has been added as following.
com.sun.org.apache.xml.internal.security.parser.pool-size 
9. JDK-8234211: Discoverable javac plugins has been allowed to be invoked by default.

10. JDK-8230814: A new method declaration has been added to SAX ContentHandler to receive notification of the XML declaration.

References

JDK 14 Release Notes
Java Language Updates
POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us