Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 3 of 4
Meanwhile, Flex came along a few years ago, and is now at version 3. Flex is a set of libraries that add important functionality like standardized GUI widgets to the Flash platform to make programming powerful Flash applications easier. Flex also adds an XML-based language called MXML that is pre-processed into ActionScript during compilation. Developers use MXML for the declarative parts of their application, such as what the GUI screens look like, and program in ActionScript directly for their core application logic. I won't cover MXML here because it is not really a part of the ActionScript language itself. I mention it because it comes up in some of the examples and language features that I will discuss later on.
I will focus on the modern features of ActionScript, sticking to what ActionScript 3 provides. Many of these features may date back to earlier versions, but to keep things simple I will just assume that we are talking about the latest version, since that is the version that most Flash and Flex developers have access to today.
I'll also start using the abbreviation "AS3" to mean ActionScript 3 now, because typing out "ActionScript 3" every time is getting tedious. When I invent a language, I'll start with as short a name as possible. Kernighan & Ritchie did it right.
I've always thought that one of the most enjoyable activities in life was comparing syntax rules of different languages. Okay, perhaps not. But it is a good place to start when comparing the languages overall, so we'll start with several simple examples of important syntactic differences between AS3 and Java.
Java declares variables like this:
public int blah;
public Object foo = new Object();
AS3 declares variables like so:
public var blah:int;
public var foo:Object = new Object();
Variable declaration is where I have tended to bruise my fingers in the Java-to-AS3 migration. After declaring thousands of
variables in Java over the years, my fingers know just what (not) to do. And after months of typing hundreds or thousands
of declarations in AS3, I still find myself having to go back and add the var keyword, or switch the type definition to live after the variable. It takes some getting used to coming from Java type-typing.
Java has no concept of "undefined":
Object foo;
Number num;
System.out.println(foo);
// outputs 'null'
System.out.println(num);
// outputs 'null'
AS3 has "undefined", "null", and "NaN" concepts:
var foo;
var bar:Object;
var num:Number;
trace(foo);
// outputs 'undefined'
trace(bar);
// outputs 'null'
trace(num);
// outputs 'NaN'
Note the use of trace() in my AS3 snippets. It's a library difference, not a language difference, so I won't discuss it here. Hopefully it is clear
that trace() is the equivalent of System.out.println(). But since outputting to the command-line still ranks as one of the developer's main weapons against bug infestation (right
above asking the person in the next cube) I thought I'd at least give a shout out to the lowly trace() statement.
Java declares packages in Java source files:
package foo;
class FooThing {
}
AS3 places classes inside package blocks:
package foo {
class FooThing {
}
}
These amount to the same thing: they are both ways to organize and control the visibility of global names. One difference is that the Java package declaration is scoped to the whole file so all classes in that file are put into that package, whereas AS3 allows multiple package definitions per file.
Scope in Java is clearly defined as being within the current block:
// using i for both is fine – completely separate scopes
public void foo() {
for (int i = 0; i < 10; ++i) {}
for (int i = 0; i < 5; ++i) {}
}
AS3 variable scope is at the level of the function itself -- regardless of whether the variable is defined within an inner block:
// causes a compiler warning
public function foo():void {
for (var i:int = 0; i < 10; ++i) {}
for (var i:int = 0; i < 5; ++i) {}
}
// better:
public function foo():void {
var i:int;
for (i = 0; i < 10; ++i) {}
for (i = 0; i < 5; ++i) {}
}
Java has annotations:
@Foo
public class Bar {
@Monkey
String banana;
}
AS3 has metadata:
[Foo]
public class Bar {
[Monkey]
var banana:String;
}
Metadata is used for declaring such things as hints to MXML, like the default property to assume when instantiating the class in MXML, and hints for tools, such as default values for properties.
Java;
requires;
semicolons;
after;
statements;
AS3
does
not
except; to; separate; multiple; statements; on; a; single; line;
But please, on behalf of the readers of your code: use semicolons anyway. After so many years of coding in other languages with line-ending syntax, it just makes code more readable, don't you think?
Java uses the final keyword for constant values:
public static final int FOO = 5;
AS3 uses the const keyword:
public static const FOO:int = 5;
Java performs casts by putting the type in parentheses before the object in question:
float f = 5;
int g = (int)f;
These parenthetical statements always look to me like the code is speaking to me quietly and discretely: "(Hey, psst! You should now consider this float to be an int. Pass it on.)"
AS3 casts look more like a function call through the type being cast to:
var f:float = 5;
var g:int = int(f);
There is also another way of casting in AS3, using the as operator:
var g:int = f as int;
Java typically declares exceptions that are thrown:
public void foo() throws MyException
{
try {
// really awesome code
} catch (Exception e) {
throw new MyException("AAAUUUGGGHHHH!");
}
}
AS3 throws exceptions without declaration:
public function foo():void
{
try {
// really awesome code
} catch (var e:Error) {
throw new Error("AAAUUUGGGHHHH!");
}
}
Java has generics and typed collections:
List<FooType> list = new ArrayList<FooType>();
list.add(new FooType());
FooType foo = list.get(0);
AS3 ... does not.
But AS3 does have typed arrays through the Vector class:
var vec:Vector.<FooType> = new Vector.<FooType>();
vec[0] = new FooType();
var foo:FooType = vec[0];
Some may wonder at the odd angle-bracket syntax of the Vector declaration. I'm not sure of the history, but I have a feeling that AS3 was just trying to achieve readability and angle-bracket
parity with Java's generics.
Speaking of angle brackets in code, Java handles XML processing through various libraries (many of them), such as JAXP, JAXB, SAX, Xerces, JDOM, etc.
AS3 has E4X integrated into the language itself for queries, manipulation, and the like.
var myXML:XML = <Grob>gable</Grob>;
trace(myXML.Grob);
// outputs 'gable'
As you've seen by now, AS3 classes look pretty much like Java classes. You haven't seen them yet, but AS3 interfaces also look eerily similar to their Java counterparts. But looks aren't everything (except with supermodels and wax fruit) and there are a few important distinctions in behavior that are worth investigating.
Java allows the same access permission specifiers (public, protected, private, and package-private) on constructors that are allowed on classes, fields, and methods:
public class FooObject {
private FooObject() {}
}
FooObject foo = new FooObject();
// Compiler error
In AS3, constructors are always public:
public class FooObject {
private function FooObject() {}
}
// Compiler error
Making a constructor private (in a language that supports it, like Java) is not a typical pattern, although it is helpful in some situations, like creating singletons. If you really want only one of something, then it's a good idea to prevent anyone but the class itself from creating it. A workaround used in AS3 involves throwing exceptions from the constructor when called from outside of your singleton accessor, but it is not quite the same thing.
Java allows properties to be declared on interfaces:
public interface IFoo {
public int blah = 5;
public final int VAL = 7;
}
*Both of these properties are implicitly static and final, even though they lack those keywords. Try it, you'll see.
AS3 does not allow properties on interfaces. Only functions can be declared on interfaces. Note, however, that you can declare properties with set/get functions; just not properties as fields. For example, this works:
public interface IProperties
{
function set blah(value:Number):void;
function get blah():Number;
}
If the get/set example here makes no sense, don't worry. You'll learn more about properties and these functions in the second half of this article.
Java allows abstract classes:
public abstract class FooObject {
public abstract void foo();
}
AS 3 ... does not. There is no concept of "abstract" in AS3.
That's probably enough comparison to get a busy mind spinning, so I'll stop for now. I hope the examples here have whet your appetite, because there's more to come. In the second half of this article we'll get into the advanced topics of properties, dynamic behavior, and functions, with a similar line-up of code-based comparison and usage commentary from, well, me.