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.
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.