AS3 Primitives

*Edit: I have found that I am wrong about the following (as shown from the comments). In AS3, numbers are in fact placed in their wrapper classes. My interpretation are wrong. You can keep read the following if you want to know how it DOESN’T work. Basically what I describe is how Java works. AVM2 doesn’t do this, it wraps everything as an object… Sort of strange as there are performence gains using primitives.

I read a post recently that asked whether AS3 Number objects are actually treated as objects in AS3.

Number, int, etc are primitives. Just like in Java, these types have their Object class associations in case you want to create instances and call various methods that come with the Number class. For example, the Number class has functions like toFixed for number precison and accuracy.

The concept of primitives are sort of confusing to newer programming as they go against object oriented programming. In fact, in Smalltalk, one of the first OOP languages, EVERYTHING is an object. The +, -, *, etc are treated as “messages”.

In Smalltalk, since everything is an object and has messages passed, you have to specify the function for “+” and “-“. Primitives in higher level languages don’t have that just for this reason. You don’t want to have to write the code to add two numbers together do you? The concatanation syntax for Strings is also “+”. Notice how “-” is not properly handled with Strings while it is with Numbers… 

Also, in terms of an ideological argument, Number can be said to be the lowest form to describe an object. Think of the composition property of object oriented design. Everything is made of objects that are made of objects that are made of objects… There are theoretically an infinite amount of layers in-between.  Object oriented design is the practice of selecting certain layers and abstracting the rest. But at the very core, every object has to be made up of primatives (more specifically, since Strings can be represented as numbers). Think of any class you’ve ever programmed. The properties will always have either primatives or other objects. If you recurisvely loop through other objects they will all also be made up of primatives (unless they have no properties at all… that’s sort of an anomoly and bad programming design dependant on “isA” relationships… so I’m going to avoid that tangent).

Primitive types are the building blocks. Just like materials are made from atoms… you can’t really go lower than Numbers in describing an object. Additionally, primitives are passed by VALUE rather by reference.

Here’s another post I wrote a while back that went a little deeper into this concept.

0 thoughts on “AS3 Primitives”

  1. “Number, int, etc are primitives.”

    Except that they are not. Everything is an object in ActionScript. Java has real primitives, but they are a completely different thing from the Number, Boolean, int and uint types in ActionScript. Just consider this:

    trace(5 is Object) // this prints “true”

    The reason that it looks like Number and the others are primitives is that the compiler and the runtime does some optimization tricks on variables that are typed as Number, int and uint, and the fact that these types are immutable. That is not the same thing as them being primitives. You can’t do this with a primitive:


    Number, int, uint and Boolean, are not passed by value, but by reference. Not that it makes any difference, because they are immutable and you can’t change them anyway. The same goes for String.

  2. Theo,
    Thanks for your response. According to the AVM2 Overview document released by Adobe:

    “Primitive data include integers and floating-point numbers encoded in various ways.”

    So I assume I’m using the correct term.

    Also, I’m confused why you say that they are not passed by reference.

    var x:int = 9;
    trace(x); // still 9
    x = func(x);
    trace(x); // now 90

    func = function(x:int)
    x = x*10;
    return x;

    The actually value is immutable because they are primitives.

    5 is Object works because of the compiler. That’s why 7345.4.toExponential works. The compiler casts the number as a Number object.

    7345.4.toExponential(2) is equivalent to Number(7345.4).toExponential(2);

  3. I think the AVM2 overview is liberal in its use of the term “primitive”, I’ve also seen mentions of “primitive objects”, which is probably a better term, since they are objects, but with some special handling where they are treated like primitives by the runtime for performance reasons.

    Your proof of them being passed by value is wrong and confuses variables for the values they contain.

    In the example “x” is a variable. It is not the object. The variable references an object, in this case the number 9. Assigning a new value to “x” makes the variable point to another object, the number 90. This was created by multiplying a reference to the earlier 9 with a literal object with the value 10, this returned a new object with the value 90.

    The example has nothing to do with the actual object being passed by value or by reference or not. Since Number and int are immutable there is no way of proving that they are passed by value or by reference in the way you do, because you only change the variable. The variable is not the same as the object it references.

    “5 is Object” and “74345.4.toExponential(4)” has absolutely nothing to do with the compiler. ActionScript has late binding, which means that the method toExponential isn’t resolved until runtime. Other languages bind early and resolve these things at compile time, but ActionScript does not. The compiler does not cast, and the runtime doesn’t cast either. The literal 5 is an object. It is just treated a bit different by the runtime in some cases.

  4. Theo,
    Thanks again for your response and the discussion.

    int x = 5 is not equivalent to int x = new Number(5) (is that what you were arguing?).

    At least in AVM2 it isn’t…

    A way you can see is by
    for (var i:int = 0; i < 100; i++)
    int x = 5;

    Since garbage collection occurs over a few frames, the instances of 5 would still be in memory if 5 referred to a new object.
    Again, I’m not sure if this is what you were arguing…

    I’m pretty sure x is referencing the memory location of value 5, rather than a new object with that value.

    Also, I’m VERY skeptical that 74345.4 in your example isn’t casted before runtime. I’m not saying I’m 100% sure I’m right; I could be wrong about this and you could be right, but I need some evidence.

    The primitives in AS3 are treated very different than objects programmers can make.

    Here’s some documentation I believe supports my claim:
    “The Number object is an object wrapper for primitive numeric values.”

    “You can manipulate primitive numeric values by using the methods and properties associated with the Number class. This class is identical to the JavaScript Number class.”

  5. Danny – Enjoyed the discussion on casual gaming SIEGE ’08. Jumping in a little late here, but, with regards to the primitives in AS3 discussion:

    “…ActionScript stores primitive values internally as immutable objects. The fact that they are stored as immutable objects means that passing by reference is effectively the same as passing by value.”

    “In ActionScript 3.0, primitive values and their wrapper objects are, for practical purposes, indistinguishable. All values, even primitive values, are objects. Flash Player and Adobe AIR treat these primitive types as special cases that behave like objects but that don’t require the normal overhead associated with creating objects. This means that the following two lines of code are equivalent:

    var someInt:int = 3;
    var someInt:int = new int(3);”

Leave a Reply

Your email address will not be published. Required fields are marked *