break and switch appears to execute all case statements

前端 未结 4 1827
南笙
南笙 2020-12-21 02:45

In the latest stable release of Java and Eclipse (Kempler), entering the following code and executing it, assuming the package and class names exist:

package         


        
4条回答
  •  温柔的废话
    2020-12-21 03:04

    When you don't put a break the switch will execute all other cases that are underneath the entry point

    So it actually executes

     x = 2;
     x = 3;
     x++;
     print(x);
     System.out.println(x);
    

    Tks to Pshemo here is a link to the specification of the switch statement

提交回复
热议问题