Java 21 Pattern Matching Tutorial 🎯

beginner
23 min

Java 21 Pattern Matching Tutorial 🎯

Welcome to our comprehensive Java 21 Pattern Matching tutorial! This lesson is designed to guide both beginners and intermediate learners on how to harness the power of pattern matching in their Java projects. Let's dive in! 🐳

What is Pattern Matching in Java? 💡

Pattern matching in Java is a feature introduced in version 21 that enables developers to match values against a pattern and extract relevant information. It simplifies working with collections, makes code more concise, and enhances readability.

Why Use Pattern Matching? 📝

Pattern matching makes your code more expressive, reduces the number of lines, and aids in better error handling. By using pattern matching, you can:

  • Extract specific elements from collections
  • Match against various types (numbers, strings, objects, etc.)
  • Check for null values easily
  • Improve switch statements with pattern matching

Basic Pattern Matching 💡

Let's start with the basics. We'll learn how to match against various data types and extract values from collections.

java
// Matching against numbers int number = 10; if (number is int) { System.out.println("It's an integer!"); } // Matching against strings String text = "Hello, World!"; if (text is String && text.startsWith("Hello")) { System.out.println("Text starts with 'Hello'."); }

In the above examples, we match against int and String types, respectively, and perform actions based on the match. The is keyword checks if the variable matches the specified type, and the && operator is used for additional conditions.

Advanced Pattern Matching 💡

Now let's dive deeper into pattern matching, using classes, static methods, and custom instances.

java
// Using classes public class Person { private String name; private int age; // Constructor, getters, and setters omitted for brevity } Person person = new Person("John Doe", 30); if (person instanceof Person) { System.out.println("It's a Person!"); Person john = (Person) person; System.out.println("Person's name: " + john.getName()); }

In this example, we create a Person class, create an instance of it, and use pattern matching to check if the variable matches the specified class. We then cast the variable to the Person class and extract values using its methods.

Pattern Matching and Switch Statements 💡

Pattern matching enhances the switch statement, making it more powerful and versatile.

java
Object value = 10; switch (value) { case 10 -> System.out.println("It's 10!"); case "10" -> System.out.println("It's the string '10'!"); default -> System.out.println("Unknown value."); }

In the above example, we use pattern matching with the switch statement to check for different types (integer and string) and perform actions based on the match. The -> symbol indicates the action to be performed for each case.

Pattern Matching Quiz 📝

Quick Quiz
Question 1 of 1

Which keyword is used to check if a variable matches a specified type in pattern matching?

Quick Quiz
Question 1 of 1

How can we use pattern matching to check for null values in Java?

That's it for our Java 21 Pattern Matching tutorial! We hope you found this lesson helpful and informative. Happy coding! 🚀

For more advanced pattern matching examples and exercises, visit CodeYourCraft's Pattern Matching in Java page. Keep learning and coding! 💻🎉