Strange behavior of Switch Case statement in Java

Posted by supernova on Stack Overflow See other posts from Stack Overflow or by supernova
Published on 2013-11-10T15:46:13Z Indexed on 2013/11/10 15:53 UTC
Read the original article Hit count: 224

Filed under:
|
    I understand that Java switch case are designed this way but why is this behavior in Java 

    int x = 1;

    switch(x){
    case 1: System.out.println(1);
    case 2: System.out.println(2);
    case 3: System.out.println(3);
    default: System.out.println("default");

    }

output : 1
         2   
         3
         default

 My question is why case 2 and 3 are executed?   I know I omitted break statement   
 but x was never 2 or 3 but case 2 and case 3 still executes?

© Stack Overflow or respective owner

Related posts about java

Related posts about switch-statement