JEP 456 Unnamed Variables

https://openjdk.org/jeps/456arrow-up-right

This demo showcases unnamed variables and unnamed patterns (_) in modern Java, using several small examples:

  • Throwaway locals: assigns to _ when a value is computed only for side effects, and demonstrates that _ can be reused.

  • Unnamed exception parameter: a catch block that intentionally ignores the exception object.

  • Ignoring extracted data: processes a queue of triples and discards the third element using _.

  • Unnamed lambda parameters: in a Map.forEach and in a callback, ignores parameters that aren’t needed.

  • Unnamed record pattern components: pattern-matches a Point and ignores one component, including a switch example.

Last updated