Class PatternMatchingInstanceofPreviewExamples

java.lang.Object
net.jrodolfo.java_evolution.java14.PatternMatchingInstanceofPreviewExamples

public class PatternMatchingInstanceofPreviewExamples extends Object
Demonstrates pattern matching for instanceof as a Java 14 preview feature.

Before this feature, type checks often required a separate cast after instanceof. That repeated the type and created room for mistakes.

Pattern matching for instanceof solves this by combining the type check and local variable binding. The feature became final in Java 16. The example uses final syntax because this repository compiles with JDK 25.

  • Constructor Details

    • PatternMatchingInstanceofPreviewExamples

      public PatternMatchingInstanceofPreviewExamples()
  • Method Details

    • describe

      public String describe(Object value)
      Uses a type pattern to bind the checked value to a local variable.
      Parameters:
      value - value to inspect
      Returns:
      a label based on the runtime type