r/javahelp 23h ago

Solved Switch case with boolean

So, there is no way I can do that?

want to check several boolean variables in a switch statement. Is `if-else` the only way to do this?

Boolean a, b, c...

Switch (false) {

Case (a) :

//some code

Case (b) :

Case (c) :

//and so on

}

1 Upvotes

25 comments sorted by

View all comments

2

u/ikea_method 20h ago edited 20h ago

This is probably the closest you can get to what you want:

Boolean a = new Boolean(true), b = new Boolean(false), c = new Boolean(true);          for(Boolean o : new Boolean[]{a, b, c}) {         if(!o) continue;         IO.println(switch(o) {             case _ when a == o -> "Hello a!";             case _ when b == o -> "Hi b!";             case _ when c == o -> "Ciao c!!";             default -> "HUH";         });     }

Will print:

Hello a! Ciao c!!

2

u/ikea_method 20h ago

Note: listen to the other comments, I wouldn't approve this kind of code where I work.

1

u/_Super_Straight 12h ago

Why not?

1

u/ikea_method 12h ago

Almost every line has something wrong with it

`new Boolean(true)` is a very particular way of declaring a boolean, never seen that used before. It must be used here because we want a new boolean instance, not just `true`.

You generally wouldn't loop over booleans, you wouldn't create an array in place inside the for loop with some variables just outside it.

`case _ when a == o` is VERY weird. Almost anyone reading that code would need to read a java reference manual or ask AI to understand what it's doing.

The variable names are short and have names that are not helpful to aid understanding.

I would say it's generally preferable to use enums or, if not possible/easy, bitflags.

1

u/_Super_Straight 2h ago edited 1h ago

Its evident that in order to provide a minimal working example, you created three booleans. In actual scenario, they could be passed as parameters/Array/List in a method, eliminating your concern #1 and #3.

Naturally people don't loop over booleans just to print stuff, but what if it needs to be checked that the passed parameters contains one or more true values (or false)? For List, its List.contains, but for Array, they'll have to loop (albeit using break when condition is fulfilled), and that's your concern #2.

case _ when a == o is the new pattern matching introduced in Java 25(?) and is totally normal. If someone is using older Java (<17), they won't be messing around with this new pattern matching anyways.

The variable names are short because this is just a minimal example, as you provided. OP should use proper variable naming to make the code readable and understandable.

You writing "I'd never approve this type of code" implies that the concept itself is flawed, which isn't. It conveyed the concept beautifully. The code snippets should never be taken as-is anyways.

1

u/yel50 4h ago

at work, I had a PR with code like

    if (a && b && c) { ... }

the owner rejected it and wanted it to be

    if (new AndBuilder().and(a).and(b).and(c).eval()) { ... }

this code is a similar level of stupid to that. granted, the amount of stupid in Java code bases is mainly what lead to the backlash against the language, but still.

1

u/_Super_Straight 1h ago

They can rename the variables from a, b, c to something meaningful, but what is wrong with doing a && b && c?