<分区>
class switch1
{
public static void main(String args[])
{
int a = 10;
switch(a)
{
default: System.out.println("Default");
case -1: System.out.println("-1");
}
}
}
我知道这个程序将执行“default”和“case -1”语句,因为在匹配条件之后没有指定 break(在本例中是在“default”之后)。
但是我不明白的是
a) 为什么 switch
语句中需要 break
?
b) 如果它所做的只是匹配,为什么它甚至执行无效匹配条件的语句(即执行“case -1”))?