switch behaviour java

Why doens't the following code throw a runtimeException?

public class Test2 extends Test {


public static void main(String[] args) {
    char[] array = new char[]{'A', '\t', 'e', 'I', 'O', 'u', '\n', 'p'};

    int count = 0;
    for (char c : array) {
        switch (c) {
            case 'A':
                continue;
            case 'E':
                count++;
                break;
            case 'I':
                count++;
                continue;
            case 'o':
                break;
            case 'u':
                count++;
                continue;
        }
    }
    System.out.println("length of array: " + array.length);
    System.out.println("count= " + count);
}

}

notice that 'E' and 'e' isn't equal and it is in the switch.. The same for 'p'. It does compile and run en prints: length of array: 8 count= 2

I completed my OCA certificate today and got the above question. But I can't figure out why it doesn't throw a runtime when 'e' or 'p' is checked.. This means there is an empty "default" in every switch or something?

Jon Skeet
people
quotationmark

This means there is an empty "default" in every switch or something?

Sort of. If no case matches the specified value, and there's no default case, nothing happens - it's as simple as that.

From section 14.11 of the JLS:

If no case matches and there is no default label, then no further action is taken and the switch statement completes normally.

I wouldn't personally have expected an exception here - I don't think I've ever worked with a language which would throw an exception in a similar language construct, though I dare say one may exist.

people

See more on this question at Stackoverflow