Class UnnamedPatternsVariablesPreviewExamples

java.lang.Object
net.jrodolfo.java_evolution.java21.UnnamedPatternsVariablesPreviewExamples

public class UnnamedPatternsVariablesPreviewExamples extends Object
Demonstrates unnamed patterns and variables as a Java 21 preview feature.

Sometimes Java syntax requires a variable name even though the program intentionally ignores the value. Before this feature, developers often used names such as ignored or unused. The underscore makes that intent part of the language.

The feature became final in Java 22 as unnamed variables and patterns. This example uses JDK 25-compatible syntax.

  • Constructor Details

    • UnnamedPatternsVariablesPreviewExamples

      public UnnamedPatternsVariablesPreviewExamples()
  • Method Details

    • countWithoutUsingElements

      public int countWithoutUsingElements(Iterable<String> values)
      Uses an unnamed variable where the value is intentionally ignored.
      Parameters:
      values - values to count
      Returns:
      how many values were visited
    • canParseInteger

      public boolean canParseInteger(String text)
      Uses an unnamed catch parameter when the exception object is not needed.
      Parameters:
      text - numeric text
      Returns:
      whether the text can be parsed