User Guide

Data types 65
Object data type
The Object data type is defined by the Object class. The Object class serves as the base class
for all class definitions in ActionScript. The ActionScript 3.0 version of the Object data type
differs from that of previous versions in three ways. First, the Object data type is no longer the
default data type assigned to variables with no type annotation. Second, the Object data type
no longer includes the value
undefined, which used to be the default value of Object
instances. Third, in ActionScript 3.0, the default value for instances of the Object class is
null.
In previous versions of ActionScript, a variable with no type annotation was automatically
assigned the Object data type. This is no longer true in ActionScript 3.0, which now includes
the idea of a truly untyped variable. Variables with no type annotation are now considered
untyped. If you prefer to make it clear to readers of your code that your intention is to leave a
variable untyped, you can use the new asterisk (
*) symbol for the type annotation, which is
equivalent to omitting a type annotation. The following example shows two equivalent
statements, both of which declare an untyped variable
x:
var x
var x:*
Only untyped variables can hold the value undefined. If you attempt to assign the value
undefined to a variable that has a data type, Flash Player will convert the value undefined to
the default value of that data type. For instances of the Object data type, the default value is
null, which means that Flash Player will convert the value undefined to null if you attempt
to assign
undefined to an Object instance.
Type conversions
A type conversion is said to occur when a value is transformed into a value of a different data
type. Type conversions can be either implicit or explicit. Implicit conversion, which is also
called coercion, is sometimes performed by Flash Player at run time. For example, if the value
2 is assigned to a variable of the Boolean data type, Flash Player converts the value 2 to the
Boolean value
true before assigning the value to the variable. Explicit conversion, which is
also called casting, occurs when your code instructs the compiler to treat a variable of one data
type as if it belongs to a different data type. When primitive values are involved, casting
actually converts values from one data type to another. To cast an object to a different type,
you wrap the object name in parentheses and precede it with the name of the new type. For
example, the following code takes a Boolean value and casts it to an integer:
var myBoolean:Boolean = true;
var myINT:int = int(myBoolean);
trace(myINT); // 1