Beginning ActionScript 3.0 – More about variablesSeptember 25, 2020
It seems that we’ve already discussed so much about the variables, but today our topic is still the variables. If you already know what the variables are, then you don’t need to read the following text. If you don’t, maybe the following text will help you get a clear concept about the variables.
What the variables are
When we compiling the following code,
- var i:int = 20;
- var msg:String = “Hello”;
The compiler knows that, you declare two variables, and then assign values to them. And the memory layout may be as follows.
Note: we divide the memory layout into two parts, one for the identifiers, and the other for values. This memory layout is just a simplify model for the actual memory layout in ActionScript 3.0. The actual memory layout is much complicated than this one.
I use this image is just want to tell you one thing, all the variables, or you can consider as identifiers, are just references to some values. When you do some operations on the variable, you actually perform the operations on the value it points to.
Suppose that you have two remote controllers, one for your TV, and the other for your air conditioner. When you perform some actions on the TV remote controller, the TV will get some changed, so the sir conditioner, but you can’t use the remote controller of your TV to control the air conditioner. Don’t say that you have an omnipotent remote controller. This is just the same in ActionScript. When you perform some actions on i, actually it performs on the value 20, and you can’t use i to do some operations on the “Hello” string.
This concept will be clearer after you’ve learned the complex values and class. Now, you just need to remember everything is object in ActionScript 3.0.
The difference between primitive values and complex values
When we discussed the data types, we divided all the data types into two kinds, one is primitive values, and the other is complex values. Do you know the reason? You may say because the primitive values seem to be simple, and the complex values seem to be complicated. Eh, that might be the reason.
As you see, the primitive values seem to be simpler than the complex values. Though we’ve introduced the complex values, you may feel their complicated from their names, such as XML, Date and so on. The difference of complexity between the primitive values and complex values results the flash player use different mechanism to treat the two kinds.
Facts speak louder than words. Let’s see the following code.
- var i:int = 20; //declare int type variable i, value 20
- var j:int = i; // assign i to j
- j = 30; //change j’s value
- trace(i); //output i
- trace(j); //output j
- //declare an array contains three item 1,2,3
- var originalArray:Array = new Array(1,2,3);
- var shadowArray:Array = array; //assign array to the shadowArray
- shadowArray = 4; //change the first element of shadowArray
- trace(originalArrayrray); //output the array
- trace(shadowArray); ////output the shadowArray
Don’t be too lazy; copy the code to your flash development tool
Here is the result run on my computer.
The conclusion we can get from this code is that, when we change the value of shadowArray, it affects the originalArray, but not so i and j.
When we declare a primitive type variable, the reference will point to an immutable object. If you declare a complex type variable, the reference just points to that kind of object, not immutable object. Immutable means unchanged.
Let’s explain the code now. At first, we declare i with value 20, the int value 20 is an immutable object, so the value 20 won’t get changed. Notice that the value 20 won’t be changed, doesn’t mean i can’t be changed. Then, we declare a new int type variable j, and assign i to j. Now, i and j points to the same int value 20.
Next, we assign j a new value 30. As you know, value 20 is an immutable object, so the flash player will generate a new immutable object value 30 and let j points to it.
Now, turn to the arrays. After we assign originalArray to the shadowArray, the memory layout might be:
Then, we change the shadowArray’s first element.
Now, you should know why the outputs like that.
Compare the two different behaviors; you can easily get a direct impression for the difference of the primitive values and the complex values.
Introduce the immutable object in ActionScript 3.0 is use for improving the performance of this language. When you pass the references of immutable object, the efficiency is almost the same with pass by value. In the other side, when you declare a primitive value many times, the flash player will only hold one copy of the value object. That means, even when you declare 10,000 strings with the same value, they will all points to the same immutable object. Obviously, it saves the memory.
Memory cost and time cost means performance.