pattern guard

An extra boolean condition attached to a pattern, so a case only matches when both the type and the condition fit.

In more detail

In a switch the guard follows the keyword when, as in case String s when s.isBlank(). With instanceof the same effect comes from combining the pattern with && and a condition. Guards keep a business rule next to the type test it refines.

Example

case String s when s.isBlank() -> "empty";

Related terms

Taught in

References

Certification courses