Class UnnamedVariablesPatternsExamples
java.lang.Object
net.jrodolfo.java_evolution.java22.UnnamedVariablesPatternsExamples
Demonstrates unnamed variables and patterns, finalized in Java 22.
Before Java 22, Java often required names for values that were intentionally unused. Those names could make code look as if the value mattered. The underscore marks the value as deliberately ignored in loops, lambdas, catch clauses, and patterns.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceSmall functional interface used to demonstrate an ignored lambda parameter. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionUses an unnamed pattern variable in a switch pattern.intcountValues(Iterable<String> values) Uses unnamed variables in a loop when only iteration count matters.intuseOnlyFirstValue(int left, int right) Uses an unnamed lambda parameter to show that one argument is intentionally ignored.
-
Constructor Details
-
UnnamedVariablesPatternsExamples
public UnnamedVariablesPatternsExamples()
-
-
Method Details
-
useOnlyFirstValue
public int useOnlyFirstValue(int left, int right) Uses an unnamed lambda parameter to show that one argument is intentionally ignored.- Parameters:
left- the useful valueright- ignored value- Returns:
- a computed value based only on the first argument
-
countValues
-
broadType
-