Created
July 3, 2014 21:46
-
-
Save gkhays/14a7e20a93612f910fcc to your computer and use it in GitHub Desktop.
Prior to JDK 1.7, you had to use an approximation to evaluate strings in a switch statement. Much easier now!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.gkh.test; | |
/* | |
* Prior to JDK 1.7, you had to use an approximation to evaluate strings in a | |
* switch statement. See the following links. | |
* http://www.xefer.com/2006/12/switchonstring | |
* http://stackoverflow.com/questions/338206/switch-statement-with-strings-in-java | |
* http://docs.oracle.com/javase/7/docs/technotes/guides/language/strings-switch.html | |
*/ | |
public class StringSwitch { | |
enum PolicyTypes { | |
EVENT, TRANSFORMATION, PUBLISHER, SUBSCRIBER | |
} | |
/** | |
* @param args | |
*/ | |
public static void main(String[] args) { | |
String policyType = "EVENT"; | |
switch (PolicyTypes.valueOf(policyType)) { | |
case EVENT: | |
System.out.println("Switch statement matched on " | |
+ PolicyTypes.EVENT); | |
break; | |
case TRANSFORMATION: | |
case PUBLISHER: | |
case SUBSCRIBER: | |
default: | |
break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment