Page 1 of 1

About Question enthuware.ocajp.i.v8.2.1357 :

Posted: Fri Oct 30, 2020 7:17 am
by Dreamweaver
Which of these combinations of switch expression types and case label value types are legal within a switch statement?

I think, the Question/Answer is not correct becasue if answer:
switch expression of type int and case label value of type char.
than the answer
switch expression of type char and case label value of type byte.
must be also correct. This is why x can be of type int or byte:

Code: Select all

final byte x = 97;
final char ch = 'a';

        switch (ch) {
            case x:
                System.out.println("hello");
        }

Re: About Question enthuware.ocajp.i.v8.2.1357 :

Posted: Sat Oct 31, 2020 11:29 am
by admin
Right, it works if the variable is a constant and its value fits into the switch variable type. But in general, byte can have a negative values and so the range of a byte is not compatible with a char. That is why switch expression of type char and case label value of type byte will not work.

This is very different from "switch expression of type int and case label value of type char." All char values are assignable to an int, so there is no issue here. Any char label will work if the switch variable is of type int.

So, the two situations are different. You cannot say that if the first is correct then the second must be correct as well.

Re: About Question enthuware.ocajp.i.v8.2.1357 :

Posted: Sat Sep 04, 2021 10:16 am
by pieterb
I believe the answer "switch expression of type int and case label value of type char." should actually be written as "switch expression of type char and case label value of type int.".

That matches with for example this code:

Code: Select all

char somechar= 'a';        
switch(somechar){ // switch expression of type char
    case 97 : System.out.println("a"); // case label value of type int
}

Re: About Question enthuware.ocajp.i.v8.2.1357 :

Posted: Sat Sep 04, 2021 10:37 pm
by admin
No, it is correct as it is. "switch expression of type char and case label value of type int" would be a wrong option because it is not valid for all situations as shown below:

Code: Select all

    int i = 97;
    char ch = 'a';

    switch(i){
       case 'a' : System.out.println("got a");//any char value is valid here because all char values are assignable to an int variable.
    }

    switch(ch){
       case 97 : System.out.println("got 97"); //This is ok.
       case -1 : System.out.println("what?"); //This will NOT compile because -1 is not a valid value for a char.

    }
Output:

Code: Select all

got a
got 96
Option 4 is also wrong because of the same reason as explained in the explanation below that option.

Re: About Question enthuware.ocajp.i.v8.2.1357 :

Posted: Sun Apr 23, 2023 10:18 am
by yuraP82
why

Code: Select all

Integer x=1;
switch(x) {
case 'a' : System.out.println("a") ;  // 'a' - do not compile
}
however

Code: Select all

Byte x=1; or Short x=1;
switch(x) {
case 'a' : System.out.println("a") ;  // 'a' - ok 
}

Re: About Question enthuware.ocajp.i.v8.2.1357 :

Posted: Sun Apr 23, 2023 1:23 pm
by admin
Sorry, no idea. Couldn't find anything in the JLS that explains this.