Variables

int number = 1;

Variables are named objects of a specified data type that we can assign a value to.

Usage

Declaring Variables

Declaring (or defining) a variable means giving it a name. You can declare a variable this way:

var variableName;

Initializing Variables

Initializing a variable means giving it an initial value. We can do just that with the = sign:

var variableName = 25;

Data Types

Each variable has a Data Type (also known as a primitive), such as...

You can initialize a variable with a specific data type if you want:

int variableName = 25;
float variableName2 = 25.01;

In most cases, the compiler will assume a number is an int. You can put an f after any number to show that it's a float, even without a decimal point:

// This will be assumed to be an int
var variableName = 25;
// This will be assumed to be a float
var variableName2 = 25f;

Conclusion

Now you know how to save whole numbers, decimals, and true/false values and refer to them by name. This will be very useful for re-using values in mathematic expressions and procedures.

With that in mind, you're ready to learn about a major component of procedures and variables.

Last updated