About Question enthuware.ocajp.i.v7.2.1357 :
Posted: Wed Dec 04, 2013 4:09 am
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
}
}
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
}
}