Pattern matching

Pattern matching will enhance the Java programming language. To start with, it will add type test patterns and constant patterns that will be supported by the switch statement and the matches expression. Later, this JEP might extend the supported patterns and language construct.

Pattern matching is an age-old technique (approximately 65 years old) that has been adapted and is used by various languages such as text-oriented, functional (Haskell), and object-oriented languages (Scala, C#).

A pattern is a combination of the following:

  • A predicate
  • A target
  • A set of binding variables

When a predicate is successfully applied to a target, a set of binding variables are extracted from the target. The patterns covered in this chapter are type test patterns and constant patterns. 

Before working with the examples in detail, let's understand what the existing issues are and why we need pattern matching.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.187.233