Skip to content

Instantly share code, notes, and snippets.

@gkhays
Created July 3, 2014 21:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gkhays/14a7e20a93612f910fcc to your computer and use it in GitHub Desktop.
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!
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