Switch statement with boolean not working?

Why does my compiler tell me:

Incompatible types:

Requires: Logical

Found: Int

in case 0 and case 1


For example:

public void test(boolean isOn){ switch (isOn){ case 0: if (isOn){ System.out.println("its on"); } break; case 1: if (!isOn){ System.out.println("its off"); } break; default: System.out.println("I don't know!"); } 

}

Driver Class:

 Club me = new Club(); me.test(true); 
+8
java switch-statement boolean
source share
8 answers

You include the boolean type, and your cases use int types. But even if you change your business to boolean types, this will not work. You cannot enable the boolean type. And that makes no sense, since using if-else would be simpler:

 if (isOn) { System.out.println("its on"); } else { System.out.println("its off"); } 

Please note that there is no "I don't know!" . The boolean type can be true or false . This is another reason why switch-case not for boolean type. There is no default case.

You can also condense it into a single statement using a conditional expression:

 public void test(boolean isOn) { System.out.println(isOn ? "its on" : "its off"); } 
+14
source share

switch (isOn) : switch boolean and want to use the case with int , e.g. case 0 !

According to JLS section 14.11 : for switch ( Expression ) SwitchBlock :

The expression may be char, byte, short, int, Character, Byte, Short, Integer, String, or an enum type another way , compile time error .

In accordance with the specification, the following rules must also be observed:

  • Each case constant expression associated with a switch statement must be assigned to an Expression expression type.
  • No two case constant expressions associated with a switch can have the same value.
  • No label switch null .
  • No more than one default label can be associated with the same switch.

Therefore, the switch statement cannot be a float, double, or boolean . To answer the question why ?: boolean true false makes sense to use with if-else , for example, if(true) then do . Floating numbers ( float , double ) are not good candidates for the switch, as accurate comparisons are often interrupted by rounding errors. for example 0.11 - 0.1 == 0.01 is false.

+3
source share

As the error is clearly indicated, the numbers are not logical.

You want true and false .

+1
source share

In Java, the switch does NOT work with Boolean.

Accepted variable types: char, byte, short, Int, String, Character, Byte, Short, Integer, enumeration

+1
source share

This is not C ++, where 1 and 0 are implicitly converted to / from true and false. Enabling booleans is also a waste of time, even if you can do it; just write a simple if / else statement or use a construct ? : ? : .

0
source share

Just convert boolean to numbers 1 and 0.

 public void test(boolean isOn){ int trueOrFalse; if(isOn == true){ trueOrFalse = 1; }else{ trueOrFalse = 0; } switch (trueOrFalse){ case 1: if (isOn){ System.out.println("its on"); } break; case 0: if (!isOn){ System.out.println("its off"); } break; default: System.out.println("I don't know!"); } } 
0
source share

Β― \ _ (ツ) _ / Β―

 switch (Boolean.hashCode(isOn)) { case 1231: System.out.println("its on"); break; case 1237: System.out.println("its off"); break; } 
0
source share

In Java, a switch only works with integer literals and those that can be converted to integer literals, such as char.

Moreover, in Java, a boolean type has only two values: true or false. This is not like the situation in C / C ++, where true is any non-zero value, and false is zero.

Also your code is a bit redundant

0
source share

All Articles