Friday, December 27, 2024
Google search engine
HomeLanguagesJavaRecord Patten with Switch in Java 19

Record Patten with Switch in Java 19

In this article, we will learn how to use the Record Pattern with Switch statements in Java 19 to create more maintainable, readable code. With the Record Pattern and Switch, we can create cleaner and more efficient code, making our programming experience more productive. As we all know switch statements help to allow for pattern matching. So we can use it for pattern matching to match against specific properties of objects.

What is Record in Java?

In Java, Record is a type of class that is designed to handle immutable data. A record is Declared using the record keyword. Here you can learn more about Record in Java.

Syntax:

public record Person(int Name, int age) {}

Here you can learn more about Java Switch statement. For example, we are going to see how can we match an Employee Record by the Name of the Employee.

Step By Step Implementation

Step 1: Creating a Java Project 

To Create a Java Project in IntelliJ Idea you can refer to Creating First Java Application in IntelliJ IDEA in this article. If you wish to use other IDE then you can also use.

Step 2: Create a Record Class

In this step, we are going to Create an Employee Record Class using the record keyword. It Contains four fields such as employee name, Age, Salary, and Designation. Below is the code for Employee Record in Java Comments are added for a better understanding of the Code.

Java




// EmployeeRecord class with four fields
public record EmployeeRecord(String name, int age,int Salary,String designation) {}


Step 3: Creating Switch Cases 

In this step, we are going to create Switch Cases to Display an Employee Record by Employee Name. Below is the function for Switch case implementation. Comments are added for Better Understanding.

Java




// funtion for print Employee Details by Employee Name 
public static void printEmployeeDetails(EmployeeRecord employee) {
        // Switch Case to match Employee Name and print Details
        switch(employee.name()) {
            case "Ramcharan": {
                System.out.println(employee.name() + " is " + employee.age() + " years old, " + "Salary = " + employee.Salary() + " Designation = " + employee.designation());
                break;
            }
            case "Chinmaya Mohapatra": {
                System.out.println(employee.name() + " is " + employee.age() + " years old, " + "Salary = " + employee.Salary() + " Designation = " + employee.designation());
                break;
            }
            case "Sam": {
                System.out.println(employee.name() + " is " + employee.age() + " years old, " + "Salary = " + employee.Salary() + " Designation = " + employee.designation());
                break;
            }
            case "Sai": {
                System.out.println(employee.name() + " is " + employee.age() + " years old, " + "Salary = " + employee.Salary() + " Designation = " + employee.designation());
                break;
            }
            default: {
                System.out.println("Employee Not Found.");
                break;
            }
        }
    }


Step 4: Calling printEmployeeDetails Function

Here we have to call the printEmployeeDetails Method prepared in the Previous step and we have to make some EmployeeRecord Object and pass these objects to the printEmployeeDetails function for Record Matching. Below is the Code. Comments are added for a Better Understanding.

Java




public class Main {
    public static void main(String args[]){
        // Creating some objects of EmployeeRecord
        EmployeeRecord ram = new EmployeeRecord("Ramcharan", 25,30000,"SDE I");
        EmployeeRecord chinmaya = new EmployeeRecord("Chinmaya Mohapatra", 30,50000,"SDE II");
        EmployeeRecord sam = new EmployeeRecord("Sam", 40,80000,"SDE II");
        EmployeeRecord sai = new EmployeeRecord("Sai", 24,60000,"SDE I");
          
        // Calling printEmployeeDetails funtion
        printEmployeeDetails(ram);
        printEmployeeDetails(chinmaya);
        printEmployeeDetails(sam);
        printEmployeeDetails(sai);
    }
    // funtion for print Employee Details by Employee Name 
    public static void printEmployeeDetails(EmployeeRecord employee) {
        // Switch Case to match Employee Name and print Details
        switch(employee.name()) {
            case "Ramcharan": {
                System.out.println(employee.name() + " is " + employee.age() + " years old, " + "Salary = " + employee.Salary() + " Designation = " + employee.designation());
                break;
            }
            case "Chinmaya Mohapatra": {
                System.out.println(employee.name() + " is " + employee.age() + " years old, " + "Salary = " + employee.Salary() + " Designation = " + employee.designation());
                break;
            }
            case "Sam": {
                System.out.println(employee.name() + " is " + employee.age() + " years old, " + "Salary = " + employee.Salary() + " Designation = " + employee.designation());
                break;
            }
            case "Sai": {
                System.out.println(employee.name() + " is " + employee.age() + " years old, " + "Salary = " + employee.Salary() + " Designation = " + employee.designation());
                break;
            }
            default: {
                System.out.println("Employee Not Found.");
                break;
            }
        }
    }
}


Output:

Ramcharan is 25 years old, Salary = 30000 Designation = SDE I
Chinmaya Mohapatra is 30 years old, Salary = 50000 Designation = SDE II
Sam is 40 years old, Salary = 80000 Designation = SDE II
Sai is 24 years old, Salary = 60000 Designation = SDE I

Process finished with exit code 0

RELATED ARTICLES

Most Popular

Recent Comments