30 August, 2006

Learn Actionscript 2.0 from Java

If you are a Java programmer, learning ActionScript 2.0 (AS2) is very easy because it is very much similar to Java 1.4. I present in this blog the syntactical differences/comparison between the languages Java 1.4 and AS2 so that by looking at the differences, you can immediately learn the AS2 syntax. But bear in mind that this post is intended to be a quick reference only if you want to learn AS2 from your existing knowledge in Java 1.4 language syntax. You still have to read more books or tutorials in order to learn AS2 fully.


Java 1.4
ActionScript 2.0
source file
•file name is MyClass.java
•multiple class per file (one class must be public)
•filename is MyClass.as
•one class per source file
declaring a class and interface
class MyClass {
/* constructor and member variables/methods are declared inside the class in any order. */
}

================

interface MyInterface extends Interface1, Interface 2 { }
all the same as Java.
implicitly public.

note: comments in ActionScript and Java are the same; the // and /* */ are all valid in AS2. The class in AS2 can also be defined as dynamic*.
declaring a constructor•MyClass() { }
•MyClass(Type param1, Type param2) { }
package-private by default.
•function MyClass() { }
•function MyClass(param1:Type, param2:Type) { }

valid access modifiers are public (the default) and private. A class can contain only one constructor function; overloaded constructor functions are not allowed in ActionScript 2.0.
declaring a method•void myMethod();
•void myMethod() { }
•void myMethod(ParamType param1, ParamType param2) { }
•ReturnType myMethod() {
return returnObj;
}
•function myMethod();
•function myMethod() { }
•private static function myMethod() { }
•function myMethod( param1:ParamType, param2:ParamType) { }
•function myMethod() : ReturnType { return returnObj;
}

no synchronized, abstract and final modifiers. no method overloading. valid modifiers are only public (the default), private, static.
declaring a member variable•Type myVariable;
•Type myVariable = new Type();
•var myVariable:Type;
•var myVariable:Type = new Type;
•private static var myVariable:Type;

valid modifiers are public (the default), private, static. no final modifier.
packaging and importingpackage pakedge;
class MyClass { }

==============

import pakedge.MyClass;
or
import pakedge.*;
class pakedge.MyClass { }

AS2 and Java has no difference with regards to importing a class or package.

note: the packaging in AS2 is very much like the packaging in Java--they're just folders. Specifying the fully-qualified-name of a class in AS2 is the same as in Java example:
var myVar:pakedge.MyClass = new pakedge.MyClass() ).
error handling
try {
} catch (Exeption ex) {
} finally {
}
try {
} catch (error:Error) {
} finally {
}

(all the same with Java)
inheritance
•class SubClass extends SuperClass { }
•class SubClass implements SuperInterface { }
•class SubClass extends SuperClass implements SuperInterface1, SuperInterface2 { }
all the same with Java.
encapsulation
•private, package-private, public
•default for members is package-private
•private and public
•default for members is public


*ActionScript 2.0 lets you define a class as "dynamic." When a class is dynamic, new properties or methods can be added at runtime into any instance of such class. (If you're familiar with C# 3.0, you know what I mean.) Some of Flash' built-in classes--for example, MovieClip, LoadVars, and SharedObject--are dynamic classes, which is why you can dynamically add properties and methods to instances of these classes.

One more notable thing is the presence of "anonymous function" and "function pointer" in AS2 but is lacking in Java. (But still one more notable thing is that Java can live without anonymous function and function pointer that's why I thank Sun for making Java simple and elegant and a kind of do-everything-with-OOP-in-mind-for-the-sake-of-consistency language :-)

What about classpath? In ActionScript, classpath is only necessary at compile time. The folder wherein your FLA file is located is the classpath. For example, if you're working on a flash document "calculator.fla" located in C:\myprojects , the C:\myprojects folder is your classpath and you must put your ActionScript source codes in there so that the ActionScript compiler can see it. The Flash IDE allows you to specify another folder to be included in your classpath. But I'm not gonna explain those here.

The this and super keywords in Java is also used similarly in AS2.

The OOP in AS2 is very much similar to Java but always remember that AS2 is also an ECMA language, so, you can still see ActionScript code that has a similar syntax like JavaScript.

Another thing, if the entry point of the program in Java application is the public static void main(String[] args) and for the applet is the public void init() method, the entry point to run a AS2 program is any action or event fired by a user (user-based actions such as "on click") or by a time event (time-based event such as "on load").

AS2 has no constants/read-only/final variables, it doesn't even have non-overridable/final methods.

There's a lot more to learn about AS2 that I leave you to discover such as its data structures, the compiler directive such as #include (I'm not kidding), the syntactic sugar for getters and setters, the delete operator, the with statement, undefine data type, the special syntax for event handling, the for...in loop, etc.

Hope this quick reference is benificial to you. :)

No comments: