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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
Dreamweaver
Posts: 32
Joined: Mon Dec 29, 2014 4:14 pm
Contact:

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

Post 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");
        }

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

pieterb
Posts: 2
Joined: Sat Sep 04, 2021 10:11 am
Contact:

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

Post 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
}

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

yuraP82
Posts: 1
Joined: Sun Apr 23, 2023 10:04 am
Contact:

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

Post 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 
}
Last edited by admin on Sun Apr 23, 2023 1:10 pm, edited 1 time in total.
Reason: Please put code inside [code] [/code]

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

Sorry, no idea. Couldn't find anything in the JLS that explains this.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 41 guests