I'm new to Java and am learning switch statements. However, the wrong case (case "two"
) seems to be matched. I suspect that it's because of the lack of break;
after case "one"
, but can someone please explain to me the logic behind Java switch statements? Why is case "two"
matched at all when the values don't even match?
Here is my code:
String a = "one";
switch(a) {
case "one": System.out.println("a is one");
case "two": System.out.println("a is two");
default: System.out.println("numbers");
}
I expected output:
a is one
numbers
But got:
a is one
a is two
numbers