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:
package sample;
/**
* Example that demonstrates how to use switch-case in Java.
* Also demonstrates that a switch cannot be performed on a
* Java String.
*/
public class SwitchTest
{
public static void testIntSwitch( final int aInt )
{
switch ( aInt )
{
case 1 : System.err.println( "One" );
break;
case 2 : System.err.println( "Two" );
break;
case 3 : System.err.println( "Three" );
break;
case 4 : System.err.println( "Four" );
break;
default : System.err.println( "No Integer Match" );
break;
}
}
public static void testStringSwitch( final String aString )
{
// The switch statement below does not compile because a Java
// String cannot be used in a switch statement. The compiler
// reports an error that looks something like this:
//
// SwitchTest.java:40: incompatible types
// found : java.lang.String
// required: int
// switch ( aString )
// ^
// 1 error
/*
switch ( aString )
{
case "One" : System.err.println( "1" );
break;
case "Two" : System.err.println( "2" );
break;
case "Three" : System.err.println( "3" );
break;
case "Four" : System.err.println( "4" );
break;
default : System.err.println( "No String Match" );
break;
}
*/
}
public static void main( String aArgs[] )
{
testIntSwitch(3);
testStringSwitch("Three");
}
}
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:
package
{
import flash.display.Sprite;
/**
* Example that demonstrates how to use switch-case in
* ActionScript. The example also demonstrates that a
* switch statement in ActionScript can switch on String
* variables.
*/
public class SwitchTest extends Sprite
{
public function testIntSwitch(aInt:int):void
{
switch ( aInt )
{
case 1 : trace( "One" );
break;
case 2 : trace( "Two" );
break;
case 3 : trace( "Three" );
break;
case 4 : trace( "Four" );
break;
default : trace( "No Integer Match." );
break;
}
}
public function testStringSwitch(aString:String):void
{
switch ( aString )
{
case "One" : trace( 1 );
break;
case "Two" : trace( 2 );
break;
case "Three" : trace( 3 );
break;
case "Four" : trace( 4 );
break;
default : trace( "No String Match." );
break;
}
}
public function SwitchTest():void
{
testIntSwitch(3);
testStringSwitch("Three");
}
}
}
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.
See "A Switch on String Idiom for Java" for more details on use of Java's enum facility to mimic String switching behavior. Another blog entry on the subject is "LA's Blog: Java String Switch."
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.
LA,
Thanks for the feedback and for your post on Java String switching.
Post a Comment