Friday, October 26, 2007

Flash verus Java: Differences

As a Java developer, moving to Flash should not be too difficult of a task. Actionscript 3.0 contains many features of the Java language, but also has tremendous amount of other features related to animations that Java does not have. It is these latter features that will require a slower learning curve for a Java developer. Here are couple of important similarities and differences that I have encountered related to the design, structure and syntax of the Actionscript language as I was reading a book. These differences are more oriented toward a Java developer moving to Actionscript as opposed to a Flash developer moving to Java.

Conceptual Differences

1. Variable declaration

Variables in Actionscript do not have a type, as opposed to Java where all variables must be assigned a type. Although one could stipulate that assigning a Java variable as a java.lang.Object could simulate an untyped Actionscript variable, the same can be done to an Actionscript variable. Although, the variable of a type Object is still a typed variable. The difference lies in the fact that an untyped Actionscript variable can be assigned a primitive data type value or an Object type, where in Java it would not be possible for a single variable to be assigned both a primitive data type and an Object at the same time.

2. Function declaration

Despite the syntactical differences, there are also two conceptual differences in function/method declarations between Actionscript and Java.

(1) The first difference is that functions do not need to declare a return type. This is to support older versions of Actionscript.

(2) The second is that functions can declare initial values for its parameters in Actionscript. Initialized parameters of a function are not required to be passed to the function, while uninitialized parameters of a function are required to be passed. Whether they will cause compile errors is something i do not yet know.

3. Function passed as value

One of the more powerful features in Actionscript is the ability to assign a variable to a reference to a function. This is quite a powerful feature in Actionscript and perhaps provides a short cut for coding.

This feature is actually used for event handling and animation, the forte of Actionscript.

4. Nested Functions

Functions can be declared within another function.

5. Package-level, Source-File-Level, And Global Functions

One can create functions within a package (package-level). Package-level functions are accessible only within the package.

Source-file-level functions are rarely used because they can be replaced with private static functions within a package.

Global functions are defined in the unnamed package and are accessible by any class or package without an import.

All these are not concepts within Java. In Java, all functions are always associated with a class.

6. Protected and default access-control modifiers.

The protected and default access modifiers of Actionscript and Java have different definitions. The protected modifier in Java allows classes within the same package and any subclasses to access the class or variable on which the protected modifier has been declared, while in Actionscript, the protected modifier allows only subclasses to access the class or variable. Therefore, the Actionscript modifier is more restrictive, but in my opinion, makes more sense.

The default modifier in Java, which is designated with no keyword but aka package-private, only allows classes within the same package to access the class or variable, while, in Actionscript, the default or internal modifier allows classes within the same package and subclasses to access the class or variable. Essentially, the internal modifier has the same access control as in Java's protected modifier.

8. Overloading Methods in Subclasses

9. No Abstract Classes

10. Reflection replaced with built-in operators.

11. Arrays are different in Actionscript.

Arrays in Actionscript are not associated with a particular class or type as in Java. So, there is only one array type in Actionscript as opposed to Java where there can be an infinite number of array types according to the type the array has been declared with. This single array type in Actionscript is used to hold all types within the same array.

The array in Actionscript is also dynamically sized and therefore will never throw an error if the initial capacity of the array has been exceeded when attempting to add new elements beyond the size of the array. In Java, an array is always initialized with an initial capacity and cannot be changed after instantiation. Instead, the ArrayList can be used in place of the fixed-sized array.

Unlike the Java array, the Actionscript array has methods used for modifying its contents. Both array types allow you to directly modify its contents using the bracket notation. However, the Actionscript array defines modification methods, listed in the following: push, unshift, splice, shift, pop, concat, delete. This allows the array to be used as a stack.

One other trivial but nice feature of the Actionscript array is that it prints out as a comma-delimited list of element values. No more weird string representations of arrays! (Athough in Java 1.5, there is a new method that prints out Arrays nicely, Arrays.toString()).

12. Dynamic Actionscript

An entirely different concept in Actionscript is the ability to dynamically create or add new instance variables and methods to classes or dynamically create new classes. There is no analogous concept in Java.

Only Actionscript classes that have been defined as

13. Actionscript Lookup Tables versus Java Collections Map

The Java Collections framework has revolutionalized the way Java developers use data structures in their programs. The lead designer of the Collections Framework, Joshua Bloch, who also wrote a highly acclaimed and one of the most useful Java programming books, called Effective Java, is a prominent Java architect among the Java community. His framework has won an award for Best Java Class Library.

One of the collection classes in the framework is a Map. There is not equivalent class in Actionscript. Instead, the ability to dynamically add instance variables to a dynamic object replaces the Map concept.

Feature Differences

1. Setter / Getter functions

2. Constants and the final modifier for instance variables.

3. The switch statement is more powerful in Actionscript.

The Java switch statement only allows the primitive data types to used as values in the switch statement. One of the complaints in Java is that you cannot use Strings or perhaps Objects within the switch statement. Fortunately, in Actionscript, you can! However, there is fine print related with this feature. In a switch statement, it uses a strict equality operator on determining equality of the switch values.

4. Actionscript has additional operators that Java does not have. Some are quite powerful.

Some of these additional operators include those that backwards-compatible for older versions of Actionscript. These will not be included here as they should not be used in new code.

5. Actionscript has built-in XML operators.

Syntactical Differences

1. Constructors

2. Overriding methods

3. void vs. null

Conceptual Similarities

1. Object-oriented programming

Actionscript 3.0 contains interfaces, classes, instance methods, static methods, instance variables, static variables.

1 comment:

ricosuave said...

jig, thanks a bunch for this treatment. Just what I needed as a javaman myself. Also useful are these links: