Unnamed Patterns and Variables in Java
Last Updated :
10 Jun, 2024
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);
}
}
}
OutputHello 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.");
}
}
}
OutputPerson's name is: Alice
Person is older than 20 years old.
Similar Reads
Using Static Variables in Java Here we will discuss the static variables in java. Java actually doesnât have the concept of Global variable. To define a Global variable in java, the keyword static is used. The advantage of the static variable is discussed below. Now geeks you must be wondering out what are the advantages of stati
3 min read
Using _ (underscore) as Variable Name in Java As we do know variables in java or rather in any language is introduced to write a code where it is suggested to give meaningful names to the variables as per their usage in code and especially in object-oriented languages are supposed to be used locally wherever it is possible instead of just using
3 min read
Static Variables in Java In Java, when a variable is declared with the static keyword. Then, a single variable is created and shared among all the objects at the class level. Static variables are, essentially, global variables. All instances of the class share the same static variable. These are the main scenarios when we u
3 min read
Final Local Variables in Java In Java, a local variable is a variable, which is declared inside a method. Local variables are only accessible within the method in which they are declared, other methods in the class do not know anything about that variable. When we declare a local variable, we need to initialize it first before u
3 min read
Final Static Variable in Java When the value of a variable is not varied, then it is not a good choice to go for an instance variable. At that time, we can add a static modifier to that variable. Whenever we declare a variable as static, then at the class level, a single variable is created which is shared with the objects. Any
3 min read
Scope of Variables in Java The scope of variables is the part of the program where the variable is accessible. Like C/C++, in Java, all identifiers are lexically (or statically) scoped, i.e., scope of a variable can be determined at compile time and independent of the function call stack. In this article, we will learn about
7 min read