Have you try to declare a variable with the var statement. If you try the following statement,
- var msg;
You will get a complier warning in the strict mode, because you haven’t designated the variable’s type. In general, you should designate a type to the variable you declare by appending the variable name with a colon (:), followed by the variable’s type. Just as
- var msg:String;
The variable msg is a String type variable now. After this designation, we can use the msg to store the String value. String is one of the data types in ActionScript 3.0.
What is data type?
A data type in programming languages is an attribute of a data which tells the computer (and the programmer) something about the kind of data it is. This involves setting constraints on the datum, such as what values it can take and what operations may be performed upon it.
For example, you can define a String type variable to describe your address, but obviously this variable is not suitable to describe your height. Further more, we can use the join operation on Strings, such as join the city and country name with your address to form the whole address, while the join operation performs on the integers may cause some strange results.
That’s to say, different types is use for describe different data, and different types have different operations.
Data types In ActionScript 3.0
In ActionScript 3.0, you can divide all data types into two sets. One is primitive value, and the other is complex value. The primitive values including “int”, “uint”, “Number”, “Boolean” and “String”; and the complex values including “Array”, “Data”, “Function”, “RegExp”, “XML”, “XMLList” and “Error”. Eh, you just need to remember the primitive values, except them are all complex values.
Primitive values
int
Remember int is short for integer, so int is use for storing integer value. In mathematics, the range of integer is from infinitely small to infinite, the range is infinite. But the memory we use to represent an integer is limit, so we can’t express all the integers with the limited memory. Actually, we use 32-bit to represent an integer in ActionScript 3.0, which means the range of integer we can represent is from -2^31 to 2^31 – 1.
Why the base is 2?
That because our computer can only recognize two digits, one and zero, is a binary system.
Why the exponent is 31, not 32?
That’ because we use one bit for sign, then the remaining bits is 31-bit, which use for representing the actual value.
Why the range seems asymmetric?
It seems that the integer values are asymmetric. That’s because this language use two’s complement to represent the integer values. You can just simply consider that’s all because we use one position for zero, so it needs to minus one
Two’s complement is widely use in programming languages, if you’re interested in it; you can visit the wikipedia for more information.
http://en.wikipedia.org/wiki/2%27s_complement
uint
uint is short for unsigned integer. That’s to say, the sign-bit is disappear, we use all 32-bit to represent the integers. And no sign-bit, no negative integers. So, as you think, the range is from 0 to 2^32 – 1.
In most cases, the int type is enough for you to represent an integer. But there is still some situations we need to use this type, uint. Such as, you can use uint to represent some integer surely not less than 0, or to represent a color. Remember int is not suitable for processing the color, use uint instead.
Overflow
We’ve already that each data type has its only value range. But what will happen if we use a data type to represent a value that over its value range, such as, we use int to store a value 2^31 (2,147,483,648).
Don’t guess, run the following code.
See the result? It’s -2147483648. Wow, it seems that some one has appended a minus sign to the value
Actually, the cause is overflow. When the value exceeds the range that the data type can represent, it will cause an overflow.
Let’s look at the following picture.
This is the range of int, from -2^31 to 2^31 – 1.
To make things simpler, you can consider the value range as a circle.
So, when you use int to store 2^31, it will becomes -2^31. And according to this circle, we will get 2^31 – 1, when we use it to store -2^31 – 1. Write some code to test it
So, the result is 2^31 – 1, as we want.
If you already knew something about the data representation in computer, you may know it clearly. If not, I hope you don’t get confused.
If you don’t understand the overflow, just remember use the proper data type to represent the value and forget the overflow.
Number
We’ve introduced two data types to represent the integers. How to represent the floating-point number? Of course, there is a corresponding type for the floating-point numbers. It’s Number.
Number type uses 64-bit to store the floating number with the IEEE-754 standard. In this standard, 1-bit for sign, figuring out whether the number is positive or negative; 11-bit for exponent; and the left bits (52-bit) are use for the mantissa.
And the range you get the range from the static properties stored in the Number class, Number.MAX_VALUE and Number.MIN_VALUE.
You can use Number to store some integer values. The Number data type uses these 52 bits and a special hidden bit to represent integers from -9,007,199,254,740,992 (-253) to 9,007,199,254,740,992 (253).
According to the official document, we should use Number data type for integer values larger than the 32-bit int and uint types can store or for floating-point numbers to maximize performance.
Boolean
You just need to know there are only two values: true and false in this type. No other values are valid for variables of Boolean type.
String
As you see in the first section, the String data type represents a sequence of 16-bit characters. Someone has said that,”The programmers spend almost 60% of their coding time dealing with the strings”, so, String type is very important, we’ll talk about it later.
Null
There is only one value, null, in this data type. This is the default value for the String data type and all classes that define complex data types.
void
Just like Null data type, there is also only one value, undefined, in this data type. Notice that, void data type can’t use for declaring variables, but you can use it for functions.
Complex values
Since the complex values are so complex, I don’t want to talk about them here, the complex values will be discuss later
.

March 15th, 2009
Ntt.cc 








Posted in
Tags: 
RSS Feed
Email Feed
[...] the first ActionScript project Variables Data type Literal and [...]
[...] the first ActionScript project Variables Data type Literal and [...]