Sub topic 1
From `instanceof` and Casts to Type Patterns
Prerequisites
This chapter assumes that you know Java classes, inheritance, interfaces, instanceof, and casts. You should also know basic if statements and Boolean operators such as && and ||. The examples use Java 21 or newer.
The older `instanceof` pattern
Before pattern matching, checking an object's type usually required two steps:
- Test the type with
instanceof. - Cast the object before using it.
static String describe(Object value) {
if (value instanceof String) {
String text = (String) value;
return "string of length " + text.length();
}
return "something else";
}The test guarantees that the cast is valid inside the if block. However, the type name appears twice, and the cast adds noise.
A type pattern combines the test, cast, and variable declaration:
static String describe(Object value) {
if (value instanceof String text) {
return "string of length " + text.length();
}
return "something else";
}String text is the pattern. It says: if value refers to a String, bind that value to the variable text. Java then treats text as a String inside the successful branch.
The pattern variable has the type named in the pattern. You can call String methods without writing a cast.
Pattern-variable scope
A pattern variable is available only where Java can prove that the pattern matched. This is called flow scoping. The compiler tracks the result of the condition instead of giving the variable ordinary block-wide scope.
A variable remains available after a successful test joined with &&:
public class PatternExample {
static String describe(Object value) {
if (value instanceof String text && !text.isEmpty()) {
return "non-empty string of length " + text.length();
}
return "something else";
}
public static void main(String[] args) {
System.out.println(describe("Java"));
System.out.println(describe(""));
System.out.println(describe(42));
}
}The right side of && runs only when the left side is true. Therefore, text is definitely a String when !text.isEmpty() is evaluated.
Negation can also make the variable available after an early return:
static int lengthOfString(Object value) {
if (!(value instanceof String text)) {
return 0;
}
return text.length();
}After the if statement, the method can continue only when the pattern matched. The compiler therefore allows text in the final return statement.
A pattern variable is not available when a condition might continue without a successful match. For example, this does not compile:
if (value instanceof String text || text.isEmpty()) {
// text may not exist when the left side is false
}With ||, the right side can be evaluated when the pattern failed, so text would not always have a value.
A practical decision
Use a type pattern when the next operation depends on the object's runtime type. If you only need a Boolean result, ordinary instanceof may be clearer.
| Need | Suitable form |
|---|---|
| Test whether a value has a type | `value instanceof String` |
| Test and use the value as that type | `value instanceof String text` |
Exercise
Why is text available in value instanceof String text && !text.isEmpty(), but not on the right side of value instanceof String text || text.isEmpty()?
Show answer
The right side of && runs only after a successful match, while the right side of || may run after the match fails.