Open In App

Record Pattern with instanceof in Java 19

Improve
Improve
Like Article
Like
Save
Share
Report

In Java, the instanceof operator is used to check whether an object is an instance of a particular class or interface. It returns a boolean value of true if the object is an instance of the specified class or interface, and false otherwise.

Example of using instanceof

Java




public class Example {
    public static void main(String[] args)
    {
        // we create an object obj of type Object
        Object obj = new String("Hello, world!");
  
        // We then use instanceof to check if obj is an
        // instance of the String class.
        if (obj instanceof String) {
            String str = (String)obj;
            System.out.println(
                "Length of the String Hello world! : "
                + str.length());
        }
    }
}


Output:

Length of the String Hello world! : 13

Explanation:

In this example, we create an object obj of type Object, which is a superclass of all other classes in Java. We then use instanceof to check if obj is an instance of the String class. Since obj contains a string, the code within the if block will be executed, and we can safely cast obj to a String to access its length() method.

Record Pattern

The record pattern is a new feature introduced in Java 16, which provides a concise way to define classes whose main purpose is to store data. A record class contains a fixed set of properties and generates a constructor, getters, and other methods to access and modify these properties automatically.

Example of a record class:

// We define a record class called Person that has
// two properties: name of type String and age of type int
public record Person(String name, int age) {}

The record keyword indicates that this is a record class.

Record Pattern with instanceof 

Using the record pattern in combination with instanceof, we can simplify code that works with record classes. 

Here’s an example:

Java




public class Example {
    public static void main(String[] args)
    {
        // create a new Person object with name "Abhi" and
        // age 25
        Person obj = new Person("Abhi", 25);
  
        // use instanceof to check if obj is an instance of
        // Person cast obj to a Person reference named
        // person if it is an instance of Person
        if (obj instanceof Person) {
            Person person = (Person)obj;
            // casting obj to Person reference
            // use the age() and name() methods to print the
            // person's name and age
            int age = person.age();
            String name = person.name();
            System.out.println(name + " is " + age
                               + " years old.");
        }
    }
}
  
class Person {
    // create private fields for name and age
    private String name;
    private int age;
  
    // create a constructor that initializes the name and
    // age fields
    public Person(String name, int age)
    {
        this.name = name;
        this.age = age;
    }
  
    // create a getter method for the name field
    public String name() { return name; }
  
    // create a getter method for the age field
    public int age() { return age; }
}


Output:

Abhi is 25 years old.

Explanation:

In this example, we create an object obj of type Object that contains a Person record object. We then use the instanceof operator with a pattern matching switch statement to check if obj is an instance of Person, and if so, bind it to a new variable person of type Person. We can then access the name and age fields directly from the person.

Conclusion

The record pattern with instanceof can help simplify code that works with record classes and make it more concise and easier to read.



Last Updated : 14 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads