r/javahelp • • Sep 10 '26

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

}

0 Upvotes

28 comments sorted by

View all comments

2

u/[deleted] Sep 10 '26 edited Sep 15 '26

[removed] — view removed comment

3

u/[deleted] Sep 10 '26 edited Sep 15 '26

[removed] — view removed comment

1

u/_Super_Straight Sep 10 '26

Why not?

1

u/[deleted] Sep 10 '26 edited Sep 15 '26

[removed] — view removed comment

1

u/_Super_Straight Sep 11 '26 edited Sep 11 '26

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/[deleted] Sep 11 '26 edited Sep 15 '26

[removed] — view removed comment

1

u/_Super_Straight Sep 11 '26 edited Sep 11 '26

Sorry I fail to understand your first half of paragraph. Are you saying we can't create boolean Array without using new Boolean or Boolean.valueOf?

boolean[] boolArr = new boolean[]{true, false, true};

Is totally valid Array declaration.

for(boolean val: boolArr){
    if(!val){
        //relevant code
        break;
    }
}

Will work without any problem.

Edit: boolArr[0] = false is also valid and permitted.

the most natural place to compute if there's at least one true boolean is when you generate the list

Agreed.

1

u/[deleted] Sep 11 '26 edited Sep 15 '26

[removed] — view removed comment

1

u/_Super_Straight Sep 11 '26
var arr = new Boolean[]{ true, false, null };

for (var x : arr) {
    switch (x) {
        case true -> methodOne();
        case false -> methodTwo();
        case null -> System.out.println("invalid");
    }
}

Is totally valid. No need to pass a, b, c as references.

1

u/[deleted] Sep 11 '26 edited Sep 15 '26

[removed] — view removed comment

→ More replies (0)

1

u/[deleted] Sep 11 '26 edited Sep 15 '26

[removed] — view removed comment

1

u/_Super_Straight Sep 11 '26

Your example is clearly better off with them being enum rather than boolean. Plus, they're being compared against their reference, not by their values.

1

u/yel50 Sep 11 '26

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 Sep 11 '26

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

1

u/iWhacko Sep 10 '26

I'm pretty sure this compares specific instances, not the value.

1

u/IceCreamInsides Sep 10 '26

I thought a switch statement would be more concise and readable than if. Interesting as concept, but ruins the goal X)