Switch Statement

Introduction: Welcome to a tutorial page on how to use a Switch statement within Java. What is a Switch statement? A Switch statement is used to check multiple cases or conditions for a particular instance. In Java 7, switch statements can be used for Strings and Integers whereas older versions of Java can only use Integers in a switch statement. When is a Switch statement used? A switch statement is very similar to a If and Else If statement except using a switch statement is much cleaner, less complex and you do not have to state the variable to check each time you want to check it - this does in-turn mean that you can only use one variable per switch statement. Examples: Setting up the methods to be created as examples:
  1. System.out.println(getMonthNumber("february"));
  2. System.out.println(getMonthString(2));
Here is an integer example from the official Java Documents to convert the month integer to a string...
  1. public static String getMonthString(int month) {
  2. String futureMonths = "";
  3. switch (month) {
  4. case 1: futureMonths = "January";
  5. break;
  6. case 2: futureMonths = "February";
  7. break;
  8. case 3: futureMonths = "March";
  9. break;
  10. case 4: futureMonths = "April";
  11. break;
  12. case 5: futureMonths = "May";
  13. break;
  14. case 6: futureMonths = "June";
  15. break;
  16. case 7: futureMonths = "July";
  17. break;
  18. case 8: futureMonths = "August";
  19. break;
  20. case 9: futureMonths = "September";
  21. break;
  22. case 10: futureMonths = "October";
  23. break;
  24. case 11: futureMonths = "November";
  25. break;
  26. case 12: futureMonths = "December";
  27. break;
  28. default: break;
  29. }
  30. return futureMonths;
  31. }
and here is a Java 7 example of turning a string in to an integer...
  1. public static int getMonthNumber(String month) {
  2. int monthNumber = 0;
  3. if (month == null) {
  4. return monthNumber;
  5. }
  6. switch (month.toLowerCase()) {
  7. case "january":
  8. monthNumber = 1;
  9. break;
  10. case "february":
  11. monthNumber = 2;
  12. break;
  13. case "march":
  14. monthNumber = 3;
  15. break;
  16. case "april":
  17. monthNumber = 4;
  18. break;
  19. case "may":
  20. monthNumber = 5;
  21. break;
  22. case "june":
  23. monthNumber = 6;
  24. break;
  25. case "july":
  26. monthNumber = 7;
  27. break;
  28. case "august":
  29. monthNumber = 8;
  30. break;
  31. case "september":
  32. monthNumber = 9;
  33. break;
  34. case "october":
  35. monthNumber = 10;
  36. break;
  37. case "november":
  38. monthNumber = 11;
  39. break;
  40. case "december":
  41. monthNumber = 12;
  42. break;
  43. default:
  44. monthNumber = 0;
  45. break;
  46. }
  47. return monthNumber;
  48. }
Which gives the combined output:
2
February
Finished!

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
2 + 7 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.