Tutorial Variables

From Monster Wiki

Jump to: navigation, search

Here's everything you need, in one example:

int i;          // Declare 'i' as an integer
int j = 2;      // Declare another integer, and assign a value
i = 3;          // Assign a new value to i
i = i + j;      // Add them together
io.writeln(i);  // Print i (which is now 5)

Types

In the above example, i was defined as an integer (int). Here's a few examples of other types you can use:

float a;              // Floating point number
uint b;               // Unsigned integer
char c = 'a';         // Character
char[] str = "hello"; // String (covered in a later tutorial)

The Monster char type is fully Unicode-enabled. Try the following:

c = '魉';             // A non-ASCII character
str = "你好";         // Non-ASCII string

(Note: copy/pasting this example won't work if your editor doesn't support Unicode!)

Variables in Monster are strongly typed. This means that you can't store a string in a variable that was defined to hold an integer, or vice versa.

A complete list of types is found in the reference manual. In addition to the basic types, there are also other types (like classes) that can be used with variables. We'll return to those in a later tutorial.

Default values

Variables are always initialized when they are declared, even if you don't specify an initial value. The default value depends on the variable type:

int i;        // default to 0
float f;      // default to nan (not a number)
char c;       // default to 0xffff (invalid Unicode character)
char[] str;   // default to "" (the empty string)
Personal tools