In a pure programming context (such as structural & OOP), what are the main differences that you find (or must be careful not to be confuse) between Java/C++ and ActionScript? Any other notable or important differences are also welcome.
views:
867answers:
5Untyped arrays. Actionscript (Dialect of ECMAScript/Javascript, so if you know that it'll be easier), has an Array class. It is untyped, just something to get used to.
Any dynamic object can be a hash table. This is actually really nice sometimes.
The syntax is pretty easy to pick up, as that's quite similar. There are some scoping differences, variables and functions are declared differently.
ECMAScript is more dynamic than Java/C++, and you may find this very useful.
If you are using Flex, try to take full advantage of MXML, it makes certain things much easier.
I'll try to add to this as things come to mind.
ActionScript 3.0 is most definitely closer to Java than it is to C++ as far as abilities go: It supports interfaces, single inheritance and metadata. It's different from both in syntax though. The biggest difference for me, moving from C/C++/Java style to ActionScript is the definition of variables. In C, to declare an int, you would do the following:
int x = 0;
Whereas in ActionScript 3 it would be done as such:
var x : int = 0;
The var is necessary, and the type is specified after the colon.
Similar changes are needed for functions. In C:
void foo(int x) { //yeah }
And in ActionScript 3:
function foo(x: int) : void { //yeah }
ActionScript 3 also supports metadata through the use of bracket operators []. The most common metadata you'll probably use is Bindable, which allows the variable following the Bindable declaration to be bound to a component which automatically updates when the variable changes.
[Bindable]
var x : int = 0;
Those are probably the most egregious differences between the C-style and AS-style that I can think of. If you're using Flex, MXML will provide a whole new set of syntax and abilities to learn as well.
Edit: Just remembered a couple of things: There is no constructor overloading in ActionScript 3. You're stuck with the default constructor. If you want to add optional parameters, you have to do so like this:
class Foo
{
public function Foo( x : number, y : int = 3, z : string = "default") : void
{ //stuff here }
}
In the above example, x is required but both y and z are optional and will be defaulted to 3 and "default", respectively.
As for your question about object classes, here is a good article detailing the differences between Java and AS3.
This will explain what class objects are - essentially every declaration of class is an instance of the Class class. It's kind of confusing, but when you get down to it, it won't really change how you program or how AS3 does OO. AS3 OO is very close to Java OO.
Classes and class hierarchies are "faked" in AS3. AS3 is closer to Javascript (which really has no concept of classes) and works with prototype based objects.
you should really check out these posts. It discusses in a good amount of detail how to go from Java to Actionscript.
http://www.javaworld.com/javaworld/jw-02-2009/jw-02-actionscript1.html?page=1
http://www.javaworld.com/javaworld/jw-03-2009/jw-03-actionscript2.html?page=1
it doesn't mention C++, but definalty worth a look.
Classes in ActionScript 3 are actually Class "objects," which is to say they're instances of the Class class, which derives from Object. See here:
http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/Class.html
And here:
http://www.adobe.com/devnet/actionscript/articles/actionscript3_overview.html
And some really interesting details here:
CookieOfFortune is correct in that the practical implications of Class deriving from Object (in terms of your day-to-day coding) are minimal -- you create instances of your classes in pretty much the same way you do in Java:
// Java
MyClass myClass = new MyClass();
// AS3
var myClass:MyClass = new MyClass();
// Or if you prefer a more loosely typed approach
var myClass = new MyClass();
But behind the scenes, ActionScript is based on the ECMAScript 3 standard, so it's actually more like a superset of JavaScript -- it's loosely typed (though it can be compiled strictly), it's prototype-oriented (even if that's abstracted from the developer: "Class, itself, declares prototype"), it supports functional as well as object-oriented paradigms (yay, closures!), and so on. It feels like Java in a lot of ways, but it's really more like JavaScript. So if C++ were off to the right, Java were in the middle, and JavaScript were on the left, the conceptual similarity between the three might look something like this:
-----------------------------------------------------------------------------
^ ^ ^ ^
JS AS3 Java C++
... not that that's a terribly useful model, necessarily. ;)
HoorayImHelping is right, though -- there are definitely some annoyances. Not only is there no constructor overloading, there's no method overloading of any kind. Events are all string-identified, which can create all kinds of problems if you're not careful. There's no such thing as an ActionScript enum. It has no concept of abstract classes. The list goes on.
But where it falls short in these departments, it excels in flexibility. So, there you have it. Hardly anything like C++, somewhat like Java, but really more like a superset of JavaScript. Hope that helps!