Java 7 : Using String in switch statement

At last java 7 started supporting String object as a parameter to switch statement.

Previously it was numbers and enumerations. I am sure this will be a welcoming change to the Java community.

[code]

public class WeekTeller {

public static void main (String [] args) {
String weekCode = args[0];
switch (weekCode) {
case "M": {
System.out.println("Monday");
break;
}
case "T": {
System.out.println("Tuesday");
break;
}
case "W": {
System.out.println("Wednesday");
break;
}
case "Th": {
System.out.println("Thursday");
break;
}
case "F": {
System.out.println("Friday");
break;
}
case "S": {
System.out.println("Saturday");
break;
}
case "Su": {
System.out.println("Sunday");
break;
}

}

}
}

[/code]

Posted in: Java, Programming

Leave a Comment