Close

Java 14 - Pattern Matching for instanceof

[Last Updated: Aug 11, 2020]

In Java 14 the instanceof operator is extended to take a type test pattern instead of just a type.

Before Java 14
if (obj instanceof String) {
    String s = (String)obj;
}
In Java 14
if (obj instanceof String s) {
    // can use s here
}

In above code if obj is an instance of String, then it is cast to String and assigned to the binding variable s.

Applying condition with && operator
if (obj instanceof String s && s.length() > 5) {
  // 
}

The binding variable s is in scope on the right hand side of the && operator, as well as in the true block. (The right hand side is only evaluated if instanceof succeeded and assigned to s.)

The same cannot be done with || operator

This is a preview language feature in JDK 14.

Java 14 IDE support: IntelliJ IDEA 2020.1 (Early Access).

Examples

package com.logicbig.example;

import java.util.List;

public class Example1 {
    public static void main(String[] args) {
        printLen("abc");
        System.out.println("--------");
        printLen(List.of("apple", "banana", "pie"));
    }

    private static void printLen(Object object) {
        if (object instanceof String str) {
            printStringLen(str);
        } else if (object instanceof List list) {
            for (Object o : list) {
                if (o instanceof String str) {
                    printStringLen(str);
                }
            }
        }
    }

    private static void printStringLen(String str) {
        System.out.printf("String: %s, Length: %s%n", str, str.length());
    }
}
String: abc, Length: 3
--------
String: apple, Length: 5
String: banana, Length: 6
String: pie, Length: 3

With &&

package com.logicbig.example;

import java.util.List;

public class Example2 {
    public static void main(String[] args) {
        printLen("abc");
        System.out.println("--------");
        printLen("x");
        System.out.println("--------");
        printLen(List.of("apple", "banana", "pie", "z"));
    }

    private static void printLen(Object object) {
        if (object instanceof String str && str.length() > 1) {
            printStringLen(str);
        } else if (object instanceof List list) {
            for (Object o : list) {
                if (o instanceof String str && str.length() > 1) {
                    printStringLen(str);
                }
            }
        }
    }

    private static void printStringLen(String str) {
        System.out.printf("String: %s, Length: %s%n", str, str.length());
    }
}
String: abc, Length: 3
--------
--------
String: apple, Length: 5
String: banana, Length: 6
String: pie, Length: 3

With null value

There are no changes to how instanceof works when the target is null. That is, the pattern will only match, and s will only be assigned, if obj is not null.

package com.logicbig.example;

import java.util.Arrays;
import java.util.List;

public class Example3 {
    public static void main(String[] args) {
        printLen(null);
        System.out.println("--------");
        printLen(Arrays.asList(null, "banana", "pie"));
    }

    private static void printLen(Object object) {
        if (object instanceof String str) {
            printStringLen(str);
        } else if (object instanceof List list) {
            for (Object o : list) {
                if (o instanceof String str) {
                    printStringLen(str);
                }
            }
        }
    }

    private static void printStringLen(String str) {
        System.out.printf("String: %s, Length: %s%n", str, str.length());
    }
}
--------
String: banana, Length: 6
String: pie, Length: 3

Example Project

Dependencies and Technologies Used:

  • JDK 14
Java 14 - Pattern Matching for instanceof Select All Download
  • java-14-instance-of-pattern-matching
    • src
      • com
        • logicbig
          • example
            • Example1.java

    See Also