Introduction to ActionScript 2.0/Primitive Data Types

After cramming all those basic concepts in your head, you deserve a break! This chapter is all about primitive data types.

What is a wrapper class?
A wrapper is a class that 'wraps' the three primitive data types with properties and methods. There are, unsurprisingly, three wrapper classes, each corresponding to a primitive data type:
 * Number
 * String
 * Boolean

Each class has a constructor function which can be used to initialise it. Initialising a number, string or Boolean value with its constructor but without any arguments will return a blank value, i.e. zero, an empty string and an empty Boolean value respectively:

Although traced as true, new Boolean is neither true nor false.

You can also put the literal into the first parameter:

There's a slight difference between a 'string' and a 'string object'. Look at the following example:

instanceof and typeof are special operators. instanceof returns true if the left operand is an instance of the right operand. In this example, 123 is not an instance of Number, but new Number(123) is. typeof returns the data type of the right operand, which can be string, number, boolean, function, movieclip or object (for non-MovieClip objects). The lowercase letters are intentional because this is what typeof returns. In our example, 123 is of the primitive data type number, but new Number(123) is an instance of the Number wrapper class.

Flash converts an instance of the wrapper class into the primitive data and vice versa whenever it is necessary. If we want to manually convert an instance of a wrapper class into a primitive data type, we can use the valueOf function:

To convert the other way, simple call the constructor function.

How can I manipulate strings?
The only string property you need to know is the length property, which shows the number of characters in the string. For example:

Note that the space is counted.

Another thing you need to know is that the position of a character in a string is called its index. If the index is a positive number, it is counted from the first character, and if the index is a negative number, it is counter from the last character. The first character is defined as 0 and the last character is defined as -1. For example, the index of 'p' in 'spot' is 1 or -3 and 'o' is 2 or -2. Now let's start manipulating strings!

How can I locate a character in a string?
The following functions are related to characters in ActionScript:
 * String.fromCharCode(code point 1, code point 2...)
 * string.charAt(index of the character);
 * string.charCodeAt(index of the character);

fromCharCode returns a string using the Unicode code points specified in the parameters. Note that we wrote 'String.fromCharCode'. That's exactly the way you're supposed to type it! This is because fromCharCode is a static method, which means it belongs to the whole class, and not just a specific string.

charAt returns a character at the desired position, while charCodeAt returns the Unicode code point of the desired character.

For example:

{{ASTrace|1= |2=>8{ o 111 }}

Remember, ActionScript uses Unicode code points, not ASCII ones, for string manipulation. ASCII code points will come in handy later.

What is a substring?
A substring is a string inside another string. There are three ways to find a substring when we know its position in a string:
 * string.substr(index of the first character, length);
 * string.substring(index of the first character[, index of the last character]);
 * string.slice(index of the first character[, index of the last character]);

If you are familiar with SQL or spreadsheets, you may have noticed find that these two functions are quite different from functions like SUBSTRING/SUBSTR/MID in SQL or spreadsheets as the string is not specified in the first parameter because it is the string that contains the method in the first place.

Note that the last character is excluded from the substring in substring and slice. If the index of the last character is not specified, Flash will return everything till the end. Here's an example with all three functions:

Sometimes, we need to find the position of a known substring inside a string. Then we need these two functions:
 * string.indexOf(substring[, index of the first character]);
 * string.lastIndexOf(substring[, index of the last character]);

indexOf is very similar to FIND in spreadsheets. It looks for the substring in the string starting from the character specified in the second parameter. If the second parameter is left blank, it starts from the beginning. The position of the first character of the first occurrence of the substring is returned. lastIndexOf is pretty much the same except it returns the last occurrence, not the first, and you specify when to stop looking, not where to start looking. The search is case-sensitive. If no occurrence is found, Flash returns -1. Example:

What are some other String methods?

 * toString(subject of conversion) is an alternative way to convert something to a string. It's pretty much the same as String(subject of conversion), which we've met back in Chapter 1.
 * concat(string1, string2...) also does pretty much the same thing as the text concatenation operator.
 * string.toUpperCase and string.toLowerCase converts the specified string to upper- and lowercase respectively. Surprising, eh?
 * string.split(delimiter, number of elements) will be covered in the next chapter.

How can I start a new line?
is a string literal that can help you start a new line. It is not enclosed with quotation marks; instead, it is concatenated with other strings.

How can I express a number in other notation systems?
ActionScript represents numbers in scientific notation in a way that is quite similar to calculators. The letter 'e' is added between the 10x portion and the number. You don't have to ensure that the first number is between 1 and 10, or add a positive (+) sign before the exponent, as Flash automatically does that for you. Flash also converts numbers to and from scientific notation when it see fits:

To express a number o hexadecimals, simply type in '0x' before the hexadecimal. Flash will convert it into denary for you:

Hexadecimals will be very useful when we deal with colours later.

What are some special numbers?
Apart from the usual floating-point decimals and integers we manipulate, ActionScript has five special numbers, all static properties of Number:
 * Number.NEGATIVE_INFINITY
 * Number.POSITIVE_INFINITY
 * Number.MAX_VALUE
 * Number.MIN_VALUE
 * Number.NaN

Like a static method, a static property is a property that belongs to the whole class, not a single instance. They are also accessed using the class name rather than the instance name. Note that these numbers are in all caps and separated by underscores (_). This is because they are constants. Although constants are variables, they are not supposed to be changed; they're just there because it would be infeasible to use literals every time.

The literals of Number.NEGATIVE_INFINITY, Number.POSITIVE_INFINITY and Number.NaN are -Infinity, Infinity and NaN respectively.

Number.MAX_VALUE and Number.MIN_VALUE are the largest integral value and smallest decimal value in Flash respectively. Number.NEGATIVE_INFINITY and Number.POSITIVE_INFINITY represent, well, negative and positive infinity respectively. Number.NaN means 'not a number'. You've probably got this value before, when you tried to treat non-numbers as numbers, such as the following:

Remember that infinity is always infinity, no matter how much you subtract from it or into how many pieces you divide it. Also remember that dividing by zero does not result in undefined or NaN, but infinity.

Two global functions, isFinite(number) and isNaN(number), can help you decide whether a number is inFinite and NaN respectively. isFinite returns true if the number is finite and false if it isn't; isNaN returns false if the parameter is a legitimate number and otherwise returns true.

Note that the last one is true because "Hello world!" is not a number (but a string). We will go into even more detail in the Math class chapter.

A word on conversion
Back in the first chapter, we learnt to convert between the primitive data types. Let's take another look at each conversion process.

Let's start with String(value). If we attempt to convert a MovieClip or TextField, it will be converted into the form 'leveln_instanceName', e.g. level0_someMovieClip refers to a MovieClip on the main timeline. If we attempt to convert an array, the resulting string will contain all the elements separated by a comma. If we attempt to convert any other object, we will get [object Object], and if we attempt to convert a function, we get [type Function].

If we put anything other than true, false or a numeral-only string into Number(value), we would get NaN.

If we put anything other than 0 or 1 into Boolean(value), we would get false if that anything is null or undefined, and true otherwise.

Another way to convert a string into an integer is to use the parseInt global function. The first parameter is a string, and the second, optional parameter is the base of the numeral system.

Note that we don't need a second parameter for the second trace because 0x already implies that the base is 16.

Conclusion
Now that we've learnt all about primitive data types, we will learn how to manipulate arrays.