default是switch的默认入口,如果没有得到合适的匹配入口,无论default 在switch中的什么位置,都会进入default
运行结果:
[table]
|default|
|default|
|case 1|
|case 2|
[/table]
package msl.ch2;
public class TestDefault {
public static void main(String[] args) {
testAfterDefault(4);
testBeforeDefault(4);
testBeforeDefault(2);
}
public static void testAfterDefault(int i)
{
switch(i)
{
case 1:
System.out.println("case 1");
break;
case 2:
System.out.println("case 2");
break;
case 3:
System.out.println("case 3");
break;
default:
System.out.println("default");
}
}
public static void testBeforeDefault(int i)
{
switch(i)
{
default:
System.out.println("default");
case 1:
System.out.println("case 1");
break;
case 2:
System.out.println("case 2");
break;
case 3:
System.out.println("case 3");
}
}
}
运行结果:
[table]
|default|
|default|
|case 1|
|case 2|
[/table]