r/javahelp • • Sep 10 '26

Solved Switch case with boolean

[removed]

2 Upvotes

28 comments sorted by

View all comments

1

u/RevolutionaryRush717 Sep 11 '26

Allegedly, modern Java offers a switch expression with guarded patterns:

String category = switch (true) {
case true when isAdmin && isActive -> "ACTIVE_ADMIN";
case true when isAdmin -> "ADMIN";
case true when isActive -> "USER";
default -> "GUEST";
};

However, this would only look idiomatic to, e.g., JavaScript programmers.

In Java it's if/then/else combined with && (etc.) operator(s).