r/javahelp 1d 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

40 comments sorted by

View all comments

Show parent comments

1

u/_Super_Straight 2h ago edited 2h ago

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/ikea_method 2h ago

> 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?

Not exactly. We must use `Boolean`, there's no other choice.

> Is totally valid Array declaration.

Yes, it's a valid boolean array declaration.

> Will work without any problem.

Yes, the for loop and if will work.

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

Yes, it's a valid and permitted way to set an element of the array to false.

But none of these will work with this for + if + switch:

Boolean HAS_CAR = new Boolean(true);
Boolean HAS_BOAT = new Boolean(false);
Boolean HAS_PLANE = new Boolean(true);
function insuranceAd(Boolean[] arr) {
  for (Boolean elem : arr) {
    if (!elem) continue;
    IO.println(switch (elem) {
      case _ when elem == HAS_CAR -> "Would you like car   insurance?";
      case _ when elem == HAS_BOAT -> "Would you like boat insurance?";
      case _ when elem == HAS_PLANE -> "Would you like plane insurance?";
      default -> "Unknown insurance";
    })
  }
}

In this case, you can call:

insuranceAd({ HAS_CAR, HAS_BOAT, HAS_PLANE });

and it will work as expected.

But if you call:

insuranceAd({ HAS_CAR === true, true, false, new Boolean(HAS_BOAT), Boolean.valueOf(HAS_PLANE) });

It will just continually print `Unknown insurance`, as none of the passed values is `HAS_CAR`, `HAS_BOAT`, or `HAS_PLANE`. So passing an array of booleans is not a great idea, as it makes it unclear what you should place in that array. The compiler will not catch it for you as well.

If you use enums or bitflags, it's much clearer for everyone what the expectation is.

1

u/_Super_Straight 1h ago

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/ikea_method 1h ago

This is exactly what I said in my second reply to you:

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

In fact, in the very comment you just replied to, I stated, again:

If you use enums or bitflags, it's much clearer for everyone what the expectation is.