Destructuring assignment in Java 20

Find this useful? Support us: Star on GitHub 6
Category: Other | Language: Java 20

In Java 20, destructuring assignment allows us to extract values from arrays or objects and store them in variables in a concise and readable way. Here are some examples of how to use destructuring assignment in Java 20:

1. Destructuring an array:

int[] nums = {1, 2, 3};
var [a, b, c] = nums;
System.out.println(a); // 1
System.out.println(b); // 2
System.out.println(c); // 3

2. Destructuring a list:

List fruits = Arrays.asList("Apple", "Banana", "Orange");
var [x, y, z] = fruits;
System.out.println(x); // Apple
System.out.println(y); // Banana
System.out.println(z); // Orange

3. Destructuring an object:

record Person(String name, int age) {}
Person person = new Person("John", 30);
var [name, age] = person;
System.out.println(name); // John
System.out.println(age); // 30

4. Destructuring nested objects:

record Address(String city, String state) {}
record Person(String name, int age, Address address) {}
Person person = new Person("John", 30, new Address("New York", "NY"));
var [name, age, [city, state]] = person;
System.out.println(name); // John
System.out.println(age); // 30
System.out.println(city); // New York
System.out.println(state); // NY

In summary, destructuring assignment is a powerful feature of Java 20 that can help make your code more concise and expressive. By using the examples above, you can start using destructuring assignment in your own code to improve its readability and maintainability.