Twitter Updates

Tuesday, 27 January 2009

Mac OS X Java Switch case broken?

I think the java switch statment is broken on OS X java 1.6 try running this.
//case_test.java
public class case_test {
public static void main(String args[]) {
int newVal = 2;
switch (newVal) {
case 0 :System.out.println(newVal + " = " + 0);
case 1 :System.out.println(newVal + " = " + 1);
case 2 :System.out.println(newVal + " = " + 2);
case 3 :System.out.println(newVal + " = " + 3);
case 4 :System.out.println(newVal + " = " + 4);
case 5 :System.out.println(newVal + " = " + 5);
}
}
}


$ javac case_test.java
$ java cast_test
2 = 2
2 = 3
2 = 4
2 = 5

Aarggh!

$ java -version
java version "1.6.0_07"
Java(TM) SE Runtime Environment (build 1.6.0_07-b06-153)
Java HotSpot(TM) 64-Bit Server VM (build 1.6.0_07-b06-57, mixed mode)

OK Answer is use break statments, which I dont have to trhe languages which I use more often.
Answer was staring me in the face if I read to the end of the lines in the Sun examples or looked at there second example. Sun Java Switch

No comments: