summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorLevi Bostian <levi.bostian@gmail.com>2015-10-12 23:04:37 -0500
committerLevi Bostian <levi.bostian@gmail.com>2015-10-12 23:04:37 -0500
commit5ae76f711e07c618c61e7a6aaca84928f2211beb (patch)
treeac8e29ee5f40760c7608236a317a97fd8f8126ff
parent574b7faf50c9ef608a3e9cf1ee88b85c05554a8b (diff)
parent35e89f86fe21eb2a1246357184628595f8edca5d (diff)
Merge pull request #1389 from cschermerhorn/master
[java/en] Add Switching on String Example to Java
-rw-r--r--java.html.markdown18
1 files changed, 18 insertions, 0 deletions
diff --git a/java.html.markdown b/java.html.markdown
index 67e5494e..653cc97a 100644
--- a/java.html.markdown
+++ b/java.html.markdown
@@ -6,6 +6,7 @@ contributors:
- ["Madison Dickson", "http://github.com/mix3d"]
- ["Simon Morgan", "http://sjm.io/"]
- ["Zachary Ferguson", "http://github.com/zfergus2"]
+ - ["Cameron Schermerhorn", "http://github.com/cschermerhorn"]
filename: LearnJava.java
---
@@ -286,6 +287,23 @@ public class LearnJava {
break;
}
System.out.println("Switch Case Result: " + monthString);
+
+ // Starting in Java 7 and above, switching Strings works like this:
+ String myAnswer = "maybe";
+ switch(myAnswer){
+ case "yes":
+ System.out.println("You answered yes.");
+ break;
+ case "no":
+ System.out.println("You answered no.");
+ break;
+ case "maybe":
+ System.out.println("You answered maybe.");
+ break;
+ default:
+ System.out.println("You answered " + myAnswer);
+ break;
+ }
// Conditional Shorthand
// You can use the '?' operator for quick assignments or logic forks.