Page 1 of 1

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

Posted: Tue Aug 21, 2012 5:32 am
by JQuirke
Here are the rules:
The type of the Expression must be char, byte, short, or int, or a compile-time error occurs.
All of the following must be true, or a compile-time error will result:
This was changed in Java 7

The switch statement compares the String object in its expression with the expressions associated with each case label as if it were using the String.equals method; consequently, the comparison of String objects in switch statements is case sensitive. The Java compiler generates generally more efficient bytecode from switch statements that use String objects than from chained if-then-else statements.

source: http://docs.oracle.com/javase/7/docs/te ... witch.html

Just so a person doesn't automatically tick the does not compile box when then see a string.

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

Posted: Thu Aug 23, 2012 8:05 am
by admin
You are right. String should be mentioned as well. This has now been updated.
thank you for your feedback!

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

Posted: Tue Oct 16, 2012 5:23 pm
by mehtapra
Just to add , Wrapper Classes for char, byte, short, int (i.e. Character, Bye, Short, and Integer) are also allowed as an argument in the switch statements as they are unboxed to the corresponding primitive types.