HTML

Strings in Switch case - Java 7 Tutorial and Example

Switch Statements are not new for any Programmer, it is available in C, C++, Java and in all major programming language. The switch statement in Java allows you to a clear, concise and efficient multiple-branch statement without lots and lots of messy if-else statement. But Java Switch and case statement has a limitation, you cannot use String in them. Since String is one of the most used class in Java, and almost every program, starting from Hello World to complex multi-tier Java application uses them, it makes a lot of sense to allow them in Switch case. In Java 6 and before, the values for the cases could only be constants of integral type e.g. byte, char, short, int and enum constants. If you consider Autoboxing then you can also use corresponding wrapper class e.g. Byte, Character, Short, and Integer.