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:
Initializing Variables
Initializing a variable means giving it an initial value.
We can do just that with the =
sign:
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:
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:
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