Unable to get behaviour of Switch case in java

后端 未结 8 750
迷失自我
迷失自我 2021-01-23 14:07

I have written small code in java 6

public class TestSwitch{

public static void main(String... args){
    int a = 1;
    System.out.println("start");
          


        
8条回答
  •  旧时难觅i
    2021-01-23 14:36

    You have not added break statement before case 2.

    Refer this to know more about fall through.

    Each break statement terminates the enclosing switch statement. Control flow continues
    with the first statement following the switch block. The break statements are necessary
    because without them, statements in switch blocks fall through: All statements after
    the matching case label are executed in sequence, regardless of the expression of
    subsequent case labels, until a break statement is encountered.
    

提交回复
热议问题