I'm trying to switch a string type value which I have made static. However I can't figure out how to put this into a switch statement I was previously using an 'if else' statement but due to the number of items I want to switch this doesn't work.
For the if else I was using `
if (item.ActivityFeedType.equals("Comment"))
for switch I;m trying to use
case (item.ActivityFeedType.equals("Comment")):
Is there something I'm missing?
switch
for Strings exists, but it's only available starting from Java 7. The syntax actually is just as with an Integer
switch:
String test = "test";
switch (test) {
case "testt":
System.out.println("Wrong");
break;
case "test":
System.out.println("Got it");
break;
}
There is no situation that a switch can handle, but a simple if-else could not. A switch simply is more convenient.
I'd recommend selecting lower-case starting letters for a Class' attributes.
Thouhg if you are working with Java 1.7,
Java's switch-case
allows only constant variable in case
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With