Java switch/case statement FAQ: Can you provide an example of a Java switch
statement syntax and structure?
Solution
Here's a sample Java method that takes an int
argument and attempts to turn that int
into a month string, using a Java switch case statement to make that decision:
private String getMonthAsString(int monthAsInt) { String monthString = null; // the java switch/case statement switch (monthAsInt) { case 1: monthString = "January"; break; case 2: monthString = "February"; break; case 3: monthString = "March"; break; case 4: monthString = "April"; break; case 5: monthString = "May"; break; case 6: monthString = "June"; break; case 7: monthString = "July"; break; case 8: monthString = "August"; break; case 9: monthString = "September"; break; case 10: monthString = "October"; break; case 11: monthString = "November"; break; case 12: monthString = "December"; break; default: monthString = "Uh-oh!"; } return monthString; }
Note that I wrote that code that way to keep it short, but it can also be written in a longer form like this:
private String getMonthAsString(int monthAsInt) { String monthString = null; // the java switch/case statement switch (monthAsInt) { case 1: monthString = "January"; break; case 2: monthString = "February"; break; case 3: monthString = "March"; break; case 4: monthString = "April"; break; case 5: monthString = "May"; break; case 6: monthString = "June"; break; case 7: monthString = "July"; break; case 8: monthString = "August"; break; case 9: monthString = "September"; break; case 10: monthString = "October"; break; case 11: monthString = "November"; break; case 12: monthString = "December"; break; default: monthString = "Uh-oh!"; } return monthString; }
A complete class that uses a switch statement
To take this Java switch/case statement example a little further, here's a complete Java class that uses this method and the corresponding switch statement:
package com.devdaily.javasamples; // a java switch case statement example public class SwitchCaseExample { public static void main(String[] args) { new SwitchExample(args[0]); } public SwitchCaseExample(String s) { int monthAsInt = Integer.parseInt(s); String monthString = getMonthAsString(monthAsInt); System.out.println("Month entered: " + monthString); } private String getMonthAsString(int monthAsInt) { String monthString = null; switch (monthAsInt) { case 1: monthString = "January"; break; case 2: monthString = "February"; break; case 3: monthString = "March"; break; case 4: monthString = "April"; break; case 5: monthString = "May"; break; case 6: monthString = "June"; break; case 7: monthString = "July"; break; case 8: monthString = "August"; break; case 9: monthString = "September"; break; case 10: monthString = "October"; break; case 11: monthString = "November"; break; case 12: monthString = "December"; break; default: monthString = "Uh-oh!"; } return monthString; } }
This class accepts a command-line argument, hopes that the argument looks something like an int
(otherwise the conversion will fail), and then converts that int
into a String. Note that the default
keyword at the bottom of the switch
statement handles and int's other than 1-12.
Java switch case statement syntax example
I hope this Java switch case statement syntax example has been helpful. As usual, if there are any questions, just leave a note in the Comments section below.