Created
July 28, 2011 20:57
-
-
Save Dru89/1112538 to your computer and use it in GitHub Desktop.
Testing strings in switch statements with Java 1.7
This file contains hidden or 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
public class StringSwitch { | |
public static void main(String... args) { | |
System.out.println("String x = new String(\"asdf\");"); | |
System.out.println("String y = new String(\"asdf\");"); | |
System.out.println("String z = \"asdf\";"); | |
String x = new String("asdf"); | |
String y = new String("asdf"); | |
String z = "asdf"; | |
System.out.format("x == y : %s\n", x==y ? "true" : "false"); | |
System.out.format("x.equals(y): %s\n", x.equals(y) ? "true" : "false"); | |
System.out.format("x == z : %s\n", x==z ? "true" : "false"); | |
System.out.format("x.equals(z): %s\n", x.equals(z) ? "true" : "false"); | |
System.out.format("y == z : %s\n", y==z ? "true" : "false"); | |
System.out.format("y.equals(z): %s\n", y.equals(z) ? "true" : "false"); | |
switch(x) { | |
case "asdf": | |
System.out.println("switch(x) case \"asdf\": true"); | |
break; | |
case "jkl;": | |
System.out.println("switch(x) Shouldn't happen."); | |
break; | |
default: | |
System.out.println("switch(x) None of the above."); | |
break; | |
} | |
switch (z) { | |
case "asdf": | |
System.out.println("switch(z) case \"asdf\": true"); | |
break; | |
case "jkl;": | |
System.out.println("switch(z) Shouldn't happen."); | |
break; | |
default: | |
System.out.println("switch(x) None of the above."); | |
break; | |
} | |
/* | |
* The following fails with the error | |
* "constant string expression required." | |
switch (x) { | |
case y: | |
System.out.println("switch(x) case y: true"); | |
break; | |
default: | |
System.out.println("switch(x) case y: false"); | |
} | |
switch (x) { | |
case z: | |
System.out.println("switch(x) case z: true"); | |
break; | |
default: | |
System.out.println("switch(x) case z: false"); | |
} | |
switch (z) { | |
case y: | |
System.out.println("switch(z) case y: true"); | |
break; | |
default: | |
System.out.println("switch(z) case y: false"); | |
} | |
switch (x) { | |
case new String("asdf"): | |
System.out.println("switch(x) case new String(\"asdf\"): true"); | |
break; | |
case new String("jkl;"): | |
System.out.println("switch(x) Shouldn't happen."); | |
break; | |
default: | |
System.out.println("switch(x) None of the above."); | |
break; | |
} | |
switch (z) { | |
case new String("asdf"): | |
System.out.println("switch(z) case new String(\"asdf\"): true"); | |
break; | |
case "jkl;": | |
System.out.println("switch(z) Shouldn't happen."); | |
break; | |
default: | |
System.out.println("switch(z) None of the above."); | |
break; | |
} | |
*/ | |
} | |
} | |
/* | |
* And now for the results: | |
* | |
* String x = new String("asdf"); | |
* String y = new String("asdf"); | |
* String z = "asdf"; | |
* x == y : false | |
* x.equals(y): true | |
* x == z : false | |
* x.equals(z): true | |
* y == z : false | |
* y.equals(z): true | |
* switch(x) case "asdf": true | |
* switch(z) case "asdf": true | |
* | |
* Failed tests: | |
* - using variables for the case in the switch statement | |
* - using new instances of the String class in the switch statement | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment