Deceptive Java switch/case block 

Joined:
07/29/2009
Posts:
13

July 29, 2009 19:30:35    Last update: July 29, 2009 19:30:35
Without the break statement, the Java switch/case block looks very deceptive.
Code:
public class TestCase {
    public static void main(String[] args) {
        switch(args.length) {
            case 0:
                System.out.println("No arguments");

            case 1:
                System.out.println("One argument");

            case 2:
                System.out.println("Two arguments");

            default:
                System.out.println("The default");
        }
    }
}


Test:
C:\tmp>java TestCase
No arguments
One argument
Two arguments
The default

C:\tmp>java TestCase 1
One argument
Two arguments
The default

C:\tmp>
Share |
| Comment  | Tags