JDK 7 - use Strings in switch statements

JDK 7 adds the ability to use String objects as the case in switch blocks. It’s not exactly an earth shattering addition but I find it quite useful in avoiding long if-then chains. Plus the release docs claim that using a switch is more efficient.

At the bottom of this post is a small java class that provides a complete example of use. Here is the part we’re concerned about. The switch takes a String argument that should be the abbreviated name of a month and sets the return value to the quarter of the year that month is in.


switch(monthIn) {
            case "Jan":
            case "Feb":
            case "Mar":
                retval = "Q1";
                break;
            case "Apr":
            case "May":
            case "Jun":
                retval = "Q2";
                break;
            case "Jul":
            case "Aug":
            case "Sep":
                retval = "Q3";
                break;
            case "Oct":
            case "Nov":
            case "Dec":
                retval = "Q4";
                break;
            default:
                throw new IllegalArgumentException("Invalid Month argument: " + monthIn);
}

That’s a pretty geedunk example but it shows the functionality and is simple to read/understand. Here is a complete class that shows the same switch statement in use:


package jdk7test;
import java.util.Calendar;

public class Jdk7test {
    public static void main(String args[]) {
        Jdk7test test = new Jdk7test();
        test.execute();
    }

    public void execute() {
        String[] strMonths = new String[] { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug",
                                            "Sep", "Oct", "Nov", "Dec" };
        Calendar cal = Calendar.getInstance();
        try {
            String monthName = strMonths[cal.get(Calendar.MONTH)];
            String quarter = getQuarter(monthName);
            System.out.println("Month: " +  monthName + " is in quarter: "
                               + quarter);
        } catch (IllegalArgumentException e) {
            System.out.println("The month sent to getQuarter was invalid");
            System.out.println(e.getMessage());
        }
    }

    public String getQuarter(String monthIn) {
        String retval;
        switch(monthIn) {
            case "Jan":
            case "Feb":
            case "Mar":
                retval = "Q1";
                break;
            case "Apr":
            case "May":
            case "Jun":
                retval = "Q2";
                break;
            case "Jul":
            case "Aug":
            case "Sep":
                retval = "Q3";
                break;
            case "Oct":
            case "Nov":
            case "Dec":
                retval = "Q4";
                break;
            default:
                throw new IllegalArgumentException("Invalid Month argument: " + monthIn);
        }
        return retval;
    }
}
Standard disclaimer - At the time of posting I believe all information contained to be accurate but there is absolutely no warranty or guarantee.

Leave a Reply

Your email address will not be published. Required fields are marked *

*


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">