First it should be stated that upon declaration of any variable, you should initialize it to a "start" value. For regular variables, it should be done direclty after declaring the variable. For objects, this should be done in the constructor. If you don't specify a default value, the computer may or may not provide one... this can cause many interesting errors that are hard to track down and can waste alot of your time!
In cannot be overstated. Whenever you create a variable, give it a reasonable initial value. For example, 0 would be a good initial value for a Number. "" (the empty string) would be a good default value for a string.
grade = 0;
name = 'Not Yet Set';
grades = [0,0,0,0];
var grade : Number = 0;
var name : String = "Not Yet Set";
var grades : Array = new Array();
int grade = 0;
char name[80] = "Not Yet Set";
int grades[100];
grades[0] = 0;
grades[1] = 0;
grades[2] = 0; // etc (USE A LOOP!)
If you forget to initialize your variables, special rules apply which are different between varying programming languages (and sometimes different based on context within the same language). Thus, see point one above: Initialize your Variables!
In ActionScript, all variables, when declared are given a default value. The wise programmer immediately assigns a specific initial value to every variable that she or he creates. The unwise programmer must memorize the following, somewhat arbitrary rules:
Variables of type "int" have a default value of 0.
Variables of type "Number" have a default value of NaN. NaN means Not a Number.
Variables of type "Boolean" have a default value of false.
Variables of type "String" are treated as Objects in this case and have a default value of null.
Variables of any "Object" type (which includes all the classes you will write) have a default value of null.
All member variables of a Newed object should be assigned a value by the objects constructor function. If not, then the above rules apply!
var an_int : int;
trace( "The variable an_int has the value: " + an_int );
// Writes "The variable an_int has the value 0" to the screen
var a_num : Number;
trace( "The variable a_num has the value: " + a_num );
// Writes "The variable a_num has the value Nan" to the screen
var a_bool : Boolean;
trace( "The variable a_bool has the value: " + a_bool );
// Writes "The variable a_bool has the value false" to the screen
var a_string : String;
trace( "The variable a_string has the value: " + a_string );
// Writes "The variable a_string has the value null" to the screen
var sprite : Sprite;
trace( "The variable sprite has the value: " + sprite );
// Writes "The variable sprite has the value null" to the screen
sprite = new Sprite();
trace( "The X location of the sprite is: " + sprite.x );
// Writes "The X location of the sprite is 0" to the screen
var firework : Firework;
trace( "The variable firework has the value: " + firework );
// Writes "The variable firework has the value null" to the screen
var object : Object;
trace( "The variable object has the value: " + object );
// Writes "The variable object has the value null" to the screen
//
// here we place an actual object into the variable object
//
var object : Object = new Object();
trace( "The variable object has the value: " + object );
// Writes "The variable object has the value [Object object]" to the screen
For object variables, the core idea is that creating the variable just provides a "bucket" to place the object into, but does not actually create an object. "Nothing" in ActionScript is represented by the keyword: null. Thus un-initialized variables contain "nothing"!
To avoid issues associated with default values (having to remember which have what) it is recommended that you always provide a value for each variable when you declare the name and type:
// Basic Type Variables
var grade_1 : int = 100;
var total : int = 0;
var height : Number = 5.2;
var passing_grade : Boolean = true;
// Object Type Variables
var grade_list : Array = new Array();
var name : String = new String("Jim"); // shortcut name = "Jim"
var sprite : Sprite = new Sprite();
var textfield : ColorPicker = new ColorPicker();
In Matlab, the only way to create a variable is to assign it a value. Thus Matlab does not have the problems associated with other languages.
grade_1 = 100; % grade_1 will have the value 100 until it is changed!
In C there are several strange rules about which variables get what. To understand what value may or may not be assigned by default to your variable would take a lecture beyond the scope of this document. Thus, the only rule you need to remember is: If you don't assign it a value, it will contain garbage. Garbage is defined as any possible number or character(s).
int x;
printf("x is %d\n");
// x is 132120991
// compile and run the program again
// x is -12312