Java JDK 21 - JEP 440 - Record Patterns


The Record Patterns feature was initially proposed as a preview feature in JDK 19 by JEP 405. It was then previewed again in JDK 20 by JEP 432. This feature evolved alongside Pattern Matching for switch (JEP 441) and is now being finalized based on ongoing experience and feedback.


What is Record Patterns?

    The Record Patterns feature to enhance the Java language's ability to deconstruct record values (Records JEP 395). These patterns allow nested and powerful data navigation and processing.

    The motivation behind Record Patterns is Pattern Matching for instanceof,

    The Pattern Matching for instanceof (JEP 305) was released as a preview feature in Java JDK 14 and was re-proposed as JEP 375 and delivered in JDK 15 as a second round of preview and was finalized as a feature in JDK 16 (JEP 394).


Examples with Pattern Matching for instanceof

    Before Java 16:

    public static void main(String[] args) {
    
            Object object = "Code2care";
    
            if (object instanceof String) {
                String str = (String) object;
                System.out.println("String Length: " + str.length());
            }
    
    }

    Java 16: Pattern Matching (As preview in Java 14)

    public static void main(String[] args) {
            Object object = "Code2care!";
    
            if (object instanceof String str) {
                System.out.println("String Length: " + str.length());
            }
    }

    As you can see in Java 16 the pattern matching makes the code concise, you can now directly use 'str' as a String here, there is no need for casting.


Examples of Record Patterns

    Example: Java 16

    public class RecordPatternsExample {
    
        record Sum(int no1, int no2) {}
    
        public static void main(String[] args) {
    
            Sum sum = new Sum(10, 20);
            printSum(sum);
        }
    
        static void printSum(Object obj) {
            if (obj instanceof Sum p) {
                int x = p.no1();
                int y = p.no2();
                System.out.println(x+y);
            }
        }
    }

    Java 21: Record Patterns

    public class RecordPatternsExample {
    
        record Sum(int no1, int no2) {}
    
        public static void main(String[] args) {
    
            Sum sum = new Sum(10, 20);
            printSum(sum);
        }
    
        static void printSum(Object obj) {
            if (obj instanceof Sum(int no1, int no2)) {
                System.out.println(no1+no2);
            }
        }

    The code with Record Patterns is more readable as it allows both checking the type and extracting components of record values in a single step. This streamlines code and eliminates the need for explicit type casting, resulting in cleaner and more maintainable code.

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap