How to use record patterns with instanceof in Java 25

Java 25 introduces improvements such as record patterns with instanceof, which allow more concise and expressive type matching and data extraction in one step. Here’s a guide on how to use them:


What are record patterns?

A record pattern enables matching and extracting components of a record class, which is essentially a class with immutable data. Record patterns simplify operations by combining type checking and field extraction syntactically.


Using instanceof with Record Patterns

In Java 25, you can use a record pattern directly with instanceof to both:
1. Match the type of the object.
2. Decompose its contents in a single expression.


Example of Record Patterns with instanceof

record Point(int x, int y) {}

public class Main {
    public static void main(String[] args) {
        Object obj = new Point(10, 20);

        // Using instanceof with a record pattern
        if (obj instanceof Point(int x, int y)) {
            System.out.println("Point coordinates: x = " + x + ", y = " + y);
        } else {
            System.out.println("Not a Point object");
        }
    }
}

Explanation

  • obj instanceof Point(int x, int y):
    • Pattern Matching: Verifies if obj is an instance of the Point record.
    • Decomposition: Extracts the x and y fields of the record into variables x and y.

As a result:

  • If obj matches the type, the fields are extracted automatically in the same step.
  • There’s no need to cast obj to Point explicitly or manually call getters.

Nesting Record Patterns

Record patterns can also be nested for more complex records containing other records or collections.

Example: Nested Record Patterns

record Rectangle(Point topLeft, Point bottomRight) {}

public class Main {
    public static void main(String[] args) {
        Object obj = new Rectangle(new Point(0, 0), new Point(10, 10));

        if (obj instanceof Rectangle(Point(int x1, int y1), Point(int x2, int y2))) {
            System.out.println("Rectangle corners: (" + x1 + ", " + y1 + ") to (" + x2 + ", " + y2 + ")");
        } else {
            System.out.println("Not a Rectangle object");
        }
    }
}

Explanation

  • Rectangle(Point(int x1, int y1), Point(int x2, int y2)) is a nested pattern:
    • Matches top-level Rectangle.
    • Decomposes its topLeft and bottomRight fields into Point objects.
    • Further extracts x and y coordinates from each Point.

Benefits

  1. Conciseness: Eliminates the need for explicit casting or redundant getter calls.
  2. Readability: Patterns declaratively show what is being matched and extracted.
  3. Flexibility: Works seamlessly with nested structures.

Good-to-Know Details

  1. Exhaustive Matching: Combine switch with record patterns for exhaustive, cleaner matching:
    void printShapeInfo(Object shape) {
       switch (shape) {
           case Point(int x, int y) -> System.out.println("Point: (" + x + ", " + y + ")");
           case Rectangle(Point topLeft, Point bottomRight) -> System.out.println("Rectangle with corners: " +
                   topLeft + " to " + bottomRight);
           default -> System.out.println("Unknown shape");
       }
    }
    
  2. Null Handling: instanceof with patterns doesn’t match null values directly. An explicit null check is still required.

  3. Restrictions: The immutability of records ensures safety and predictability when decomposing data and matching patterns.


Conclusion

The introduction of record patterns in Java 25 significantly enhances pattern matching and makes working with immutable objects far more intuitive and concise. Whether you’re matching simple records or nested structures, this feature saves you from boilerplate code and improves code readability.

How do I use enhanced instanceof pattern matching?

Enhanced instanceof pattern matching, introduced in Java 16 (as a preview feature) and finalized in Java 17, allows you to combine type checking with type casting, reducing boilerplate code and making it more concise and readable.

Here’s how you can use enhanced instanceof pattern matching:

  1. Basic Usage:
    Instead of separately checking if an object is an instance of a class and then casting it, you can do both in one step using the pattern matching feature. The syntax is:

    if (obj instanceof Type variableName) {
       // variableName is automatically cast to Type
    }
    

    Example:

    Object obj = "Hello, Java!";
    
    if (obj instanceof String str) { // This checks and casts obj to String
       System.out.println("String length: " + str.length());
    } else {
       System.out.println("Not a string.");
    }
    

    This eliminates the need for explicit type casting.

  2. Combine with Logical Operators:
    You can combine the pattern matching with additional conditions using logical operators like && or ||.

    Example:

    Object obj = "Patterns in Java";
    
    if (obj instanceof String str && str.length() > 10) {
       System.out.println("String is longer than 10 characters: " + str);
    } else {
       System.out.println("String is too short or not a string at all.");
    }
    

    In this case, the str variable is only in scope if both conditions are true.

  3. Scope of the Pattern Variable:

    • The pattern variable (e.g., str in the examples above) is only accessible within the block where the pattern matching is true.
    • Outside of the if block, the variable doesn’t exist.
  4. Negating with !instanceof:
    Pattern matching itself cannot be negated directly (no “not instanceof”), but you can invert the condition like this:

    if (!(obj instanceof String)) {
       System.out.println("Not a string.");
    }
    
  5. Using Pattern Matching in switch:
    Starting from Java 17 (as a preview) and improved in later versions, you can use pattern matching in switch statements for more powerful expressions. For example:

    Object obj = "Java 17";
    
    switch (obj) {
       case String str && str.length() > 5 -> System.out.println("Long string: " + str);
       case String str -> System.out.println("Short string: " + str);
       default -> System.out.println("Not a string.");
    }
    

    This allows a combination of pattern matching and conditionals directly within switch.


Benefits of Enhanced instanceof Pattern Matching

  • Reduction of Boilerplate Code: By avoiding explicit casting and declaring new variables.
  • Improved Readability: Simplifies conditional checks by combining the instance check and cast in one step.
  • Type Safety: Provides better compile-time safety for the variables you use after a cast.

Recap of the Code Features

From the files you’ve referenced:

  1. PatternMatchingExample.java demonstrates simple pattern matching with instanceof, where the type check and assignment are done in one step.
  2. PatternMatchingExampleCombine.java shows combining pattern matching with additional conditions (e.g., &&).

Both examples illustrate the practical and concise approach to type checking and casting introduced via enhanced instanceof pattern matching.

How to Use Pattern Matching with instanceof in Java 17

Pattern matching with the instanceof operator was introduced in Java 16 (as a preview feature) and became a standard feature in Java 17. It simplifies the process of type casting when checking an object’s type, making the code shorter and more readable.

Here’s how you can use pattern matching with instanceof in Java 17:

Syntax

With pattern matching, you can directly declare a local variable while checking the type with instanceof. If the condition is true, the variable is automatically cast to the specified type, and you can use it without explicit casting.

if (object instanceof Type variableName) {
   // Use variableName, which is already cast to Type
}

Key Features:

  1. Type Checking and Casting in One Step: No need for an explicit cast.
  2. Shorter Code: Reduces boilerplate.
  3. Available Within Scope: The variable is accessible only within the scope of the if block where the condition is evaluated as true.
  4. Guarded Pattern (Available in Java 20+ – Preview): Introduced later, allowing additional conditions within instanceof.

Example 1: Basic Usage

package org.kodejava.basic;

public class PatternMatchingExample {
   public static void main(String[] args) {
      Object obj = "Hello, Java 17!";

      if (obj instanceof String str) {
         // Type already checked and cast to `String`
         System.out.println("String length: " + str.length());
      } else {
         System.out.println("Not a string.");
      }
   }
}

Explanation:

  • The variable str is declared and automatically cast to String in the same instanceof statement.
  • Within the if block, you can directly use str as it is guaranteed to be a String.

Example 2: Pattern Matching in Loops

package org.kodejava.basic;

import java.util.List;

public class PatternMatchingExample {
   public static void main(String[] args) {
      List<Object> objects = List.of("Java", 42, 3.14, "Pattern Matching");

      for (Object obj : objects) {
         if (obj instanceof String str) {
            System.out.println("Found a String: " + str.toUpperCase());
         } else if (obj instanceof Integer num) {
            System.out.println("Found an Integer: " + (num * 2));
         } else if (obj instanceof Double decimal) {
            System.out.println("Found a Double: " + (decimal + 1));
         } else {
            System.out.println("Unknown type: " + obj);
         }
      }
   }
}

Output:

Found a String: JAVA
Found an Integer: 84
Found a Double: 4.14
Found a String: PATTERN MATCHING

Example 3: Combining && Conditions

You can combine pattern matching with additional conditions:

package org.kodejava.basic;

public class PatternMatchingExample {
   public static void main(String[] args) {
      Object obj = "Hello";

      if (obj instanceof String str && str.length() > 5) {
         System.out.println("String is longer than 5 characters: " + str);
      } else {
         System.out.println("Not a long string (or not a string at all).");
      }
   }
}

Notes:

  1. Scope of Variable:
    The variable introduced inside the instanceof is only accessible inside the block where the condition is true. For example:

    if (obj instanceof String str) {
       System.out.println(str); // str is available here
    }
    // System.out.println(str); // ERROR: str not available here
    
  2. Null Safety:
    If the object being matched is null, the instanceof check will return false, so you don’t have to handle nulls manually.

Benefits:

  • Simplifies code structure.
  • Eliminates the need for verbose casting.
  • Improves readability and reduces errors associated with unnecessary manual typecasting.

Pattern matching with instanceof is now widely used in modern Java. Make sure you’re using JDK 17 or later to take advantage of this feature!

How do I use the instanceof keyword?

To check whether an object is of a particular type (class or interface type) you can use instanceof operator. The instanceof operator is used only for object reference variable. x instanceof y can be read as x is-a y.

The instanceof returns true if the reference variable being tested is of the type being compared to. It will still return true if the object being compared is assignment compatible with the type on the right.

For interface type, an object is said to be of a particular interface type (meaning it will pass the instanceof test) if any of the object’s superclasses implement the interface.

package org.kodejava.basic;

interface Man {
}

public class InstanceofDemo {
    public static void main(String[] args) {
        Body body = new Body();
        Hand hand = new Hand();
        Nail nail = new Nail();
        Shoes shoe = new Shoes();

        if (body instanceof Man) {
            System.out.println("body is a Man");
        }

        if (hand instanceof Man) {
            System.out.println("hand is a Man too");
        }

        if (hand instanceof Body) {
            System.out.println("hand is a Body");
        }

        // it should be return false
        if (hand instanceof Nail) {
            System.out.println("hand is a Nail");
        } else {
            System.out.println("hand is not a Nail");
        }

        if (nail instanceof Man) {
            System.out.println("nail is a Man too");
        }

        if (nail instanceof Hand) {
            System.out.println("nail is a Hand");
        }
        if (nail instanceof Body) {
            System.out.println("nail is a Body too");
        }

        // it should return false, cause Shoes is not implements Man
        if (shoe instanceof Man) {
            System.out.println("shoe is a Man");
        } else {
            System.out.println("shoe is not a Man");
        }

        // compile error. cannot test against class in different
        // class hierarchies.
        //
        //if (shoe instanceof Body) {
        //}

    }

}

class Body implements Man {
}

// indirect implements Man
class Hand extends Body {
}

// indirect implements Man
class Nail extends Hand {
}

class Shoes {
}

The result of the code snippet above:

body is a Man
hand is a Man too
hand is a Body
hand is not a Nail
nail is a Man too
nail is a Hand
nail is a Body too
shoe is not a Man

How do I know the class of an object?

For instance, you have a collection of objects in an List object, and you want to do some logic based on the object’s class. This can easily be done using the instanceof operator. The operator returns true if an object is an instance of a specified class, if not it will return false.

The instanceof operator is most likely used when implementing an equals(Object o) method of an object to check if the compared object is from the same class.

package org.kodejava.lang;

import java.util.ArrayList;
import java.util.List;

public class InstanceOfExample {
    public static void main(String[] args) {
        Person person = new Person("John");
        Animal animal = new Animal("Highland");
        Thing thing = new Thing("Red");
        String text = "hello";
        Integer number = 1000;

        List<Object> list = new ArrayList<>();
        list.add(person);
        list.add(animal);
        list.add(thing);
        list.add(text);
        list.add(number);

        for (Object o : list) {
            if (o instanceof Person) {
                System.out.println("My name is " + ((Person) o).getName());
            } else if (o instanceof Animal) {
                System.out.println("I live in " + ((Animal) o).getHabitat());
            } else if (o instanceof Thing) {
                System.out.println("My color is " + ((Thing) o).getColor());
            } else if (o instanceof String) {
                System.out.println("My text is " + o.toString());
            } else if (o instanceof Integer) {
                System.out.println("My value is " + ((Integer) o));
            }
        }
    }
}

class Person {
    private final String name;

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

    public String getName() {
        return name;
    }
}

class Animal {
    private final String habitat;

    public Animal(String habitat) {
        this.habitat = habitat;
    }

    public String getHabitat() {
        return habitat;
    }
}

class Thing {
    private final String color;

    public Thing(String color) {
        this.color = color;
    }

    public String getColor() {
        return color;
    }
}

The result of the code snippet above:

My name is John
I live in Highland
My color is Red
My text is hello
My value is 1000