Data types

A data type defines a set of values. For example, the Boolean data type is the set of exactly two values: true and false . In addition to the Boolean data type, ActionScript 3.0 defines several more commonly used data types, such as String, Number, and Array. You can define your own data types by using classes or interfaces to define a custom set of values. All values in ActionScript 3.0, whether they are primitive or complex , are objects.

A primitive value is a value that belongs to one of the following data types: Boolean, int, Number, String, and uint. Working with primitive values is usually faster than working with complex values, because ActionScript stores primitive values in a special way that makes memory and speed optimizations possible.

Note: For readers interested in the technical details, 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. This cuts down on memory usage and increases execution speed, because references are usually significantly smaller than the values themselves.

A complex value is a value that is not a primitive value. Data types that define sets of complex values include Array, Date, Error, Function, RegExp, XML, and XMLList.

Many programming languages distinguish between primitive values and their wrapper objects. Java, for example, has an int primitive and the java.lang.Integer class that wraps it. Java primitives are not objects, but their wrappers are, which makes primitives useful for some operations, and wrapper objects better suited for other operations. 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);

All the primitive and complex data types listed above are defined by the ActionScript 3.0 core classes. The core classes allow you to create objects using literal values instead of using the new operator. For example, you can create an array using a literal value or the Array class constructor, as follows:

var someArray:Array = [1, 2, 3]; // literal value 
var someArray:Array = new Array(1,2,3); // Array constructor