From the course: Pattern Matching for Switch in Java 21
Unlock this course with a free trial
Join today to access over 25,500 courses taught by industry experts.
Using guarded pattern case labels - Java Tutorial
From the course: Pattern Matching for Switch in Java 21
Using guarded pattern case labels
- [Instructor] A guarded pattern case label contains a type pattern and a Boolean expression, which is known as the guard. The when clause is used to specify the Boolean expression or the guard condition. Here's how it's done. I'm commenting the first example and now writing it again like this. First, let's command the first example, writing a new switch expression again, return switch of obj, put the semicolon at the end of the switch expression, then start adding the case labels. First case label, case employee e. Then to add the guard condition, type the when clause. When, now you can give the guard condition, for example, to check if the employee salary is greater than or equal to a 100,000. So I'm typing e.getSalary is greater than or equal to 100,000. Then add the arrow operator and give the expression that needs to be returned. For convenience, I'm copying it from the first example and pasting it there. Highly paid with the salary of, the salary formatted as a float. At the…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.