- A variable is an item of data used to store the state of objects.
- A variable has a:
-data type - The data type indicates the type of value that the variable can hold.
-name - The variable name must follow rules for identifiers.
Declaring Variables
- Declare a variable as follows:
[=initial value]; - Note: Values enclosed in <> are required values, while those values in [] are optional.
Coding Guidelines
- It always good to initialize your variables as you declare them.
- Use descriptive names for your variables. Like for example, if you want to have a variable that contains a grade for a student, name it as, grade and not just some random letters you choose.
- Declare one variable per line of code. For example, the variable declarations,
double exam=0;
double quiz=10;
double grade = 0;
is preferred over the declaration,
double exam=0, quiz=10, grade=0;