Sunday, July 29, 2012

Java 7 new feature switch-case for String

In Java 7, you can use String in switch-case conditional statement:

package javaexercise;

/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class JavaExercise {

    public static void main(String[] args) {
            String result;
            result = doSwitch("Monday");
            System.out.println(result);
        }
    
    private static String doSwitch(String in){
        String out;
        switch (in){
            case "Sunday":
                out = "Sunday";
                break;
            case "Monday":
                out = "Monday";
                break;
            case "Tuesday":
                out = "Tuesday";
                break;
            case "Wednesday":
                out = "Wednesday";
                break;
            case "Thursday":
                out = "Thursday";
                break;
            case "Friday":
                out = "Friday";
                break;
            case "Saturday":
                out = "Saturday";
                break;
            default:
                out = "unknown!";
        }
        return out;
    }
}


No comments:

Post a Comment