Files
javademos/lanes/src/UnnamedVariables.java
T

22 lines
860 B
Java

/**
* "Unnamed patterns and unnamed classes are also final in 25" is only half true. Unnamed variables and patterns (the
* underscore) were finalised in Java 22 (JEP 456). What became final in 25 is compact source files and instance main
* methods (JEP 512), the successor to "unnamed classes". This file checks the underscore half.
* Chapter: docs/14-lanes-21-to-25.md
*/
public class UnnamedVariables {
record Point(int x, int y) {}
public static void main(String[] args) {
Object o = new Point(3, 4);
if (o instanceof Point(int x, _)) {
System.out.println("matched a Point, x = " + x + ", y ignored with _");
}
try {
Integer.parseInt("nope");
} catch (NumberFormatException _) {
System.out.println("caught NumberFormatException without naming it");
}
}
}