Page 1 of 1

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

Posted: Wed Dec 04, 2013 4:09 am
by ewebxml
For option a)
a. switch expression of type int and case label value of type char.

I did not select option a) because you cannot assign an 'int' to a 'char'
without an explicit cast.

Based on the code below you can switch on an int and use 'char' case labels since this code compiles.

I assume the JLS has this documented somewhere.
// ----- class SwitchInt
public class SwitchInt {
public static void main(String[] args) {
int i = 11;
switch (i) {
case 'c':
case 'd':
case 'e':
case 11:
System.out.println("Input was 11");
}

// When not using a switch
int j = 3;
char c = 1;
c = (char)j; // requires explicit cast
}
}

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

Posted: Tue Feb 11, 2014 9:50 am
by Kipchakbaev

Code: Select all

case: 200 :                //some code;
case: 300 :                //some code;
Bad syntax in code in explanation - extra colon in case

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

Posted: Tue Feb 11, 2014 10:34 am
by admin
Fixed.
thank you for your feedback!