Saturday, January 3, 2015

Switch Operators Some Interesting facts

Note: The below understanding is a collection from numerous articles which have been browsed thru the web to get the understanding.
References will be provided at the end.

SWITCH CASE:
Since JavaSE7 onwards this is one statement which works with the following data-types-
  1. byte
  2. short
  3. int
  4. char
  5. enum types
This also works with following wrapper classes-
  1. Byte
  2. Short
  3. Integer
  4. Character
Some good features:
  • default statement is optional.
  • default statement can be at the beginning and before case statements
  • break statements are useful and recommended. If these are not used then next statements can get executed.
  • presence of default statement helps to handle conditions at run-time, which otherwise can return unexpected results.
  • One can use loops, if-then-else statements within case/default.
  • Multiple case statements can be clubbed together and one result generated. 
  • In case this is enclosed in a for, do-while or while loop, then a break followed by loop name can break the loop;