Loading topic
instanceof. It is the first step toward pattern matching in switch and record patterns, which later chapters build on it. By the end you will replace instanceof-and-cast code with type patterns, and you will know where the binding variable is in scope a6 min readswitch statements, switch expressions with arrows, instanceof with casts, and basic inheritance. It also assumes you have read the previous chapter on instanceof patterns. You need JDK 21 or newer.4 min readswitch statements and switch expressions, the instanceof operator, and the type patterns from the previous two chapters. You need JDK 21 or newer to run the examples.4 min readcase Integer i ->, and that you know what a record and a sealed interface are. You need JDK 21 or newer. By the end you will be able to add a when guard to a case, predict which case runs, and fix the compile erro6 min readwhen guards, as covered in the earlier chapters on pattern labels and guards. It also assumes you know basic subtyping, such as String implementing CharSequence. You need JDK 21 or newer to compile the examples.5 min readswitch. You need JDK 17 or newer to compile sealed types. Pattern switch needs JDK 21.5 min readswitch handles every possible input, and why leaving out default is often the safer choice. It assumes you know sealed interfaces, records, type patterns in switch and when guards from the earlier chapters, and that you have JDK 21 or newer5 min readinstanceof and switch. The data side is a kind of type whose shape the compiler can see. In Java, records are that data side. This chapter explains what a record guarantees and which of those guarantees pattern matching relies on. It also cov5 min readcase Point(int x, int y)). It also assumes you are comfortable with sealed interfaces, when guards, and dominance, and that you have JDK 21 or newer. Everything here is final in Java 21, except unnamed patterns, which this chapter d4 min readswitch expressions, sealed interfaces, and record patterns from the earlier chapters. You need JDK 21 or newer. The examples use only features that are final in Java 21.6 min readwhen guards, dominance, sealed interfaces and record patterns. It needs JDK 22 or n4 min read