Unnamed Patterns and Variables in Java

Java programming language offers multiple features that allow developers to write rich and concise code. Among these features, there are unnamed patterns and variables which enables developers to work with data in a more flexible and elegant way.

In this article, we will study about unnamed patterns and variables in Java and how they can be used effectively.

Unnamed variables

These are related features to unnamed patterns which allow developers to use placeholder variables without assigning names to them explicitly. This can be useful in various situations where a variable name is not necessary or the value of variable is not going to be used.

Example of Unnamed Variables:

Similar to unnamed patterns, Unnamed variables are denoted by the underscore character ‘_’.

// unnamed variables
int _ = 10;

In the above example, the ‘_’ is used as a placeholder for each element in the names list. Since the value of the variable is not used within the loop, using an unnamed variable makes the code more concise.

Below is an example of an Unnamed Variables:

Java
// Java Program to Illustrate the Use
// Of Unnamed Variables
import java.util.Arrays;
import java.util.List;

// Driver Class
public class UnnamedVariables {
    // Main Function
    public static void main(String[] args) {
        List<String> names = Arrays.asList("Shivansh", "Ramesh", "Aakash");

        // Using a valid variable name to iterate over the list
        for (String name : names) {
            System.out.println("Hello " + name);
        }
    }
}

Output
Hello Shivansh
Hello Ramesh
Hello Aakash

Unnamed Patterns

Patterns in Java are mainly associated with switch expressions and instanceof operators. Unnamed patterns that are introduced in Java 14 as preview features and made stable in Java 16 which extend the concept of patterns by allowing developers to match values without binding them to variables explicitly.

Example of Unnamed Pattern

It uses the underscore character ‘_’ as a wildcard to match any value without binding it to a variable. In the above example, the ‘_’ pattern matches any value of ‘x’ that is not explicitly handled by the previous cases. It acts as a wildcard pattern.

Below is an example of an Unnamed Pattern:

Java
// Define a class Person with a constructor
class Person {
    String name;
    int age;

    // Constructor with parameters for name and age
    Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
}

// Driver Class
public class UnnamedPatterns {
    // Main Function
    public static void main(String[] args) {
        // Create an instance of the Person class
        Person person = new Person("Alice", 30);

        // Use pattern matching in an if statement
        if (person instanceof Person) {
            // Cast person to Person type to access its fields
            Person p = (Person) person;
            System.out.println("Person's name is: " + p.name);
        }

        // Another example with pattern matching and an
        // unnamed variable
        if (person instanceof Person && person.age > 20) {
            System.out.println("Person is older than 20 years old.");
        }
    }
}

Output
Person's name is: Alice
Person is older than 20 years old.

Frequently Asked Question

1. When Should I use unnamed patterns and variables?

Unnamed patterns and variables are useful in cases where we need to match values and iterate over the collections without explicitly naming the variable. We can use them when the variable names are not crucial to understand the code or when you want to write more concise code.

2. Can We use unnamed patterns and variables in all java versions?

Unnamed patterns are introduced as a preview feature in Java 14 and they are made stable in Java 16. So, you can you them in only Java 16 or later versions.

3. Are unnamed patterns and variables recommended for all situations?

Unnamed patterns and variables in Java make your code more concise and rich, they should be used wisely. Because overusing them can make your code harder to understand and difficult to read for other developers. Use them where they improve readability and maintainability.



Contact Us