Quick glance

Java 14 features — peek glance

  1. Pattern matching instanceof
  2. Record
  3. Switch expression
  4. text blocks

Pattern matching instanceof

Pattern matching instanceof simplifies the identification of type of the object and type casting. Find the syntax & example below.

/**
* Pattern Matching Instance of.
*/
public class InstanceOfExample {
public static void main(String[] args) {
Object input = 12;

//before Java 14.
if (input instanceof Integer) {
Integer number = (Integer) input;
System.out.println(number);
}

//After Java 14.
if (input instanceof Integer number) {
System.out.println(number);
}

}
}

Record

Record mainly reducing the getter and setter methods in our pojos and its the preview feature. Find the example below and added screenshot from my intellij. Lombok jar have annotation @Data to create getter/setter but now we can use the Record.

Intellij create new record
public class RecordExample {

public static void main(String[] args) {

//before java 14
BeforeJava14 beforeJava14 = new BeforeJava14();
beforeJava14.setVersion(12);
System.out.println(beforeJava14.getVersion());

//After Java 14
AfterJava14 afterJava14 = new AfterJava14(14);
System.out.println(afterJava14.version());

}

}
//using the record
record AfterJava14(Integer version){}



class BeforeJava14 {
private Integer version;

public Integer getVersion() {
return version;
}

public void setVersion(Integer version) {
this.version = version;
}

Lambda switch expression

Java 14 made switch expression as a functional way as we followed imperative for so many years after Java 8. Here is the example below

public class SwitchExpressionExample {

public static void main(String[] args) {
int statusCode = 200;

//Before Java 14
String status = null;
switch (statusCode) {
case 200, 201, 204:
status = "SUCCESS";
break;
case 400, 500:
status = "FAILURE";
break;
default:
throw new IllegalStateException("Unexpected value: " + statusCode);
}
System.out.println(status);

statusCode = 400;
//After java 14
status = switch (statusCode) {
case 200, 201, 204 -> "SUCCESS";
case 400, 500 -> "FAILURE";
default -> throw new IllegalStateException("Unexpected value: " + statusCode);
};


System.out.println(status);
}
}

Text Blocks

All might fed up with the Java append string ‘+’ signs, that make our code little clumsy. This feature is quite interesting, just specify the string with in the three quotes.

public class TextBlocksExample {

public static void main(String[] args) {

//Before java 14
String utterences = "User is interacting " +
"with the " +
"customer";
System.out.println(utterences);


//After java 14
utterences = """
User is interacting with the customer.
cool
bye
""";


System.out.println(utterences);

}

Find the examples in following git repo https://github.com/saravanastar/java14

Software Enginner

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store