Java language

instanceof type pattern

Tests a reference value and initializes a pattern variable on success. A null reference does not match. Java 16+.

Formal Syntax & Structure

java syntaxjava
value instanceof String text

Return Value

boolean

Whether the value matches the pattern.

Working Example

Example Snippetjava
public class Main {    static int textLength(Object value) {        if (value instanceof String text && !text.isBlank()) {            return text.length();        }        return 0;    }    public static void main(String[] args) {        System.out.println(textLength("Map"));        System.out.println(textLength(" "));        System.out.println(textLength(42));        System.out.println(textLength(null));    }}
Java Reference