From Java to Flex In (n + 1) Easy Steps

| 2 Comments
Much has been made about ActionScript 3 (AS3) being a fully mature object-oriented language on par with older languages like Java, C++ or SmallTalk. I can't say that I agree, but I can attest to the many syntactical similarities between Java and AS3. Those similarities can be very misleading, though, since they can make you unwary of fundamental differences bwtween the two languages.

Here are a few key differences between JAva and AS3 that I have discovered:

Flex Object Equality

In Java, developers quickly learn the difference between the "==" operator and a call to an object's equals() method. In Flex, there are two equality operators, "==" (normal equals) and "===" (memory address equals). When dealing with primitives, things are pretty straight forward. If, however, you are dealing with more complex objects, you may want to consider using the built-in object comparator method ObjectUtil.compare().

Have a look here for more details: http://danielroop.com/blog/2007/09/02/actionscript-30-object-equality/

Variable "Block" Scoping in Flex

In Java, the location where a variable is declared makes it easy to determine the scope within which that variable will be visible. If a variable is declared within a for block, for instance, that variable is invisible once you get past the curly bracket at the end of the for loop. Not so in Flex. Variables within a block are visible to all of the function within which they are declared, even outside of the block they are in.

Therefore: do not re-use variable names in a given Flex function or you will eventually get burned.

Useful reference: http://marxsoftware.blogspot.com/2008/10/no-block-scope-in-actionscript.html

Cast Operator

In Flex, casting an object to a Class type is done using the "as" operator.

var someVar:ClassTypeA = new ClassTypeA();
var someOtherVar:ClassTypeB;
someOtherVar = someVar as ClassTypeB;

Flex equivalent to Java instanceof operator

Flex's equivalent to the Java "instanceof" operator is the "is" operator. As far as I know (which isn't saying much), it seems to behave the same as its Java counterpart.

if (someVar is ClassTypeA) {
    trace("someVar is an instance of a ClassTypeA.");
}

// Assuming that the cast succeeds
if (someVar as ClassTypeB is ClassTypeB) {
    trace("This should always be true.");
}

That's pretty much all that comes to mind for the time being, but there's surely more to come later. Please feel free to add your own Java/Flex differences in the comments section.

2 Comments

Nice article. This would be a great resource if you could expand it a bit to include things like the omission of the abstract keyword, casting objects using ClassTypeB(someVar) notation and the like.

I agree, Brett. The omission of abstract classes is a big blind spot in the Actionscript language. I've personally run into a few cases where I had to cut-and-paste code (gasp!) across classes because I couldn't make their parent abstract. Also, the two means of casting (the "as" and what I call the "constructor" casting syntax) and the differences between them is probably worth its own article.

I'll get on it as soon as I have time... hopefully soon.

Leave a comment


About this Entry

This page contains a blog entry by Taylor Bastien published on October 27, 2008 10:17 AM AD.

Something you might not have known about filtered DataGrids was the previous entry in this blog.

Excellent Resource For Flex Learners is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.