Differences between Java and ActionScript3

Recently I set out to start writing a Adobe Flex application, which uses ActionScript3 as the native language. I looked for advice on where to start and a few people told me it’s a lot like Java, and that I should just start writing Java code and the few differences would be picked up by the compiler. So I started my project, and I’ve found for the most part, my advisors were correct: ActionScript3 is very similar to Java. But there are differences.

One of the main differences, one that you’ll either love or you’ll hate, is optional static typing. Basically you are not forced to type your variables like you are in Java, but you have the liberty to do so if you want. Here is the equivalent code in Java and ActionScript3:

Java:
public class TestVariableDefinitions {

public String id;
public int numOccurances;
public Object xmlData;
public Object jsonData;

// …

ActionScript:
public class TestVariableDefinitions
{
// these two are statically typed
public var id:String;
public var numOccurances:int;

// these two are dynamically typed
public var xmlData;
public var jsonData:*;

The second difference, which can be seen in the code example above, is syntax:

  • Variables are defined with the var keyword
  • Variable types are optional (dynamic typing)
  • Methods are declared explicitly with the function keyword
  • Method header type hints are signified with a colon, not a space

Another difference is in the line breaks for braces. In ActionScript3, it is convention that braces for methods, classes, loops, etc., all get their own line, whereas in Java, the opening braces are on the same line as the header. You can see this in the code example above.

Last is the concept of getters and setters. While the Java syntax works in ActionScript3, there is a new syntax that I feel helps readability slightly, and acts as a “magic” method. This is demonstrated below:

Java:

public class TestVariableDefinitions {

public static String id;

public static void main(String[] args){
System.out.println(“The ID is ” + getId());
}

public static String getId(){
return TestVariableDefinitions.id;
}
}

Direct Translation to ActionScript3:
public class TestVariableDefinitions
{
public var id:String;

public function TestVariableDefinitions() {
trace(“The ID is ” + getId());
}

public function getId():String {
return id;
}

}

Using alternate syntax:
public class TestVariableDefinitions
{
public var _id:String;

public function TestVariableDefinitions() {
trace(“The ID is ” + id);
}

public function get id():String {
return _id;
}

}

Notice how we were able to access a magic variable named id, without that variable actually having to exist. The getter (and setter), automatically picks up on the fact that you are trying to access a special property of the object. This works similarly with setters:

public class TestVariableDefinitions
{
public var _id:String;

public function TestVariableDefinitions() {
var myNumber:int = 5;
id = 5;
}

public function set id(number:int) {
_id = number;
}

}

Note that it is not possible (because it wouldn’t make any sense) to have a class property and magic setter/getter referring to the same property name.

There are other small differences, but these are the ones I found to be most prominent. If there are any big differences between the two languages that you feel I missed, feel free to share.