Dustin's Pages

Thursday, November 8, 2007

Java and ActionScript: switch-case and Switching on Strings

It is relatively easy for an experienced Java developer to start using ActionScript because of the many similarities in semantics and syntax. However, there are differences between them as well and sometimes these differences are subtle. Also, sometimes these differences remind one that each language has some features that are desirable but not provided in the other language.

Both Java and ActionScript support the switch-case paradigm for more compact and readable if-then-else conditional handling. This blog entry focuses on how the switch-case is mostly the same for Java and ActionScript. In one major area, the languages differ on their treatment of the switch-case statement paradigm.

The next code listing shows how a Java switch statement is used. The code block that is commented out does not compile because Java switch statements do not support switching on Strings. Here is the SwitchTest.java code listing:

The code listing above for SwitchTest.java not only shows how to use a switch statement in Java, but more importantly demonstrates the compiler error that is reported if one tries to switch on a String.

To build and run the above code sample, follow these steps:

1. javac -d . SwitchTest.java 2. java sample.SwitchTest

The next code listing shows ActionScript's use of a switch statement. Unlike Java, ActionScript allows a switch to occur on a String. This example demonstrates this and the function that switches on String does not need to be commented out. Here is the code listing for SwitchTest.as:

The code in the above listing can be compiled and run by using the following steps:

1. mxmlc -debug=true SwitchTest.as [need debug set to see trace() output] 2. fdb [run the Flex debugger to see trace() output; probably need to use "run", "continue", and "continue"] 3. Open compiled SwitchTest.swf file in web browser with Flash Debug player installed. 4. View trace() output in window in which fdb was run.

It is very convenient to be able to switch on Strings in ActionScript. I used to really miss this feature in Java switch statements, but the Java enum introduced with Java 5 has largely removed my desire for a switch statement in Java that supports String switching. ActionScript does not have an enum type and so it is nice that it supports String switching even if String switching does not make up for all of the advantages of the enum.

I have found that effective use of enums has largely removed my need to switch on an String and I have been able to take advantage of polymorphism associated with the enum to have a cleaner solution than a String switch could ever be. When writing Java, I heavily use enums and with ActionScript I use switches on Strings.

2 comments:

Hey man.Thanks for referring to my post on Java string switching.I too miss the ability to have Java do string switching like in C#, was very useful for some of the parsers I wrote.The thing about using Enums, is that you have to update 2 areas if you are adding more keywords to parse through.