views:

867

answers:

5

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.

A: 

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

CookieOfFortune
I heard that in AS3, classes are more than just classes, they are more called as "class-objects". I did not understand this.
It's not really a big deal, classes still work in pretty much the same way.
CookieOfFortune
+1  A: 

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.

Hooray Im Helping
can you be more clear. I heard that in AS3, classes are more than just classes, they are more called as "class-objects". I did not understanf this.
A: 

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.

an0nym0usc0ward
can you be more clear. I heard that in AS3, classes are more than just classes, they are more called as "class-objects". I did not understanf this.
A contrast between Java and Javascript is given in http://peter.michaux.ca/articles/transitioning-from-java-classes-to-javascript-prototypesIn Actionscript, you can use "class"es, but the MXMLC compiler is massaging the code to use prototype based object definitions.
an0nym0usc0ward
what makes you think that classes are faked in AS3. AS2 was like javascript, but AS3 is more like java. Read the links in the answer I've given. AS3 is a proper 'grown up' language now :)
kenneth
check out https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Class-Based_vs._Prototype-Based_LanguagesAS3 is most definatly class based, AS2 would have been considered prototype based.
kenneth
+1 This is actually absolutely true.
Christian Nunciato
I'd like to see some articles or evidence that makes you suggest that AS3 fakes classes and is closer to JavaScript. The only part in my opinion that could be considered to be not strictly class based is when you use the ‘dynamic’ keyword so that you can add properties to a class at runtime. And the practice of using ‘dynamic’ is not recommended unless 100% necessary as its slower and more likely to generated bugs.
kenneth
Thought I'd add this quote from the live docs 'Note: In ActionScript 3.0, prototype inheritance is not the primary mechanism for inheritance. Class inheritance, which drives the inheritance of fixed properties in class definitions, is the primary inheritance mechanism in ActionScript 3.0.'
kenneth
Adobe provides some background/history here:http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7f3f.html
an0nym0usc0ward
A: 

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.

kenneth
A: 

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:

http://livedocs.adobe.com/flash/9.0/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00000069.html

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!

Christian Nunciato
I should add (and this is totally my opinion just from having used both languages, among others, as well as previous versions of ActionScript) that ActionScript 3 was almost surely designed to appeal to Java developers. If you're comfortable in Java, Adobe's gone to great lengths to help you make the transition to ActionScript, so that transition be quite painless.
Christian Nunciato