Review of Identifiers and Variables
An identifier is a name for something.
A variable is a name for a location in memory used to hold a data value.
When you declare a variable, you are instructing the compiler to reserve
a portion of main memory space large enough to hold a particular type of
value and indicating the name by which you will refer to that location.
Example 1: These are valid variable declarations:
int x, y, z;
Exercise 4: What are the types and what are the variables in the
above declarations? Which of the above variables represent primitive types?
Which represent objects? How are their memory locations represented?
The name of the type has to come before the variable name.
The types represented in the above declarations are: int, double,
boolean, String, and char, respectively. Only String
represents an object type. The primitive types are listed in the next section.
By convention object types start with an uppercase letter. Variable names should
always start with a lowercase letter. The memory location for a variable of
primitive type directly holds its value, while the memory location for
a variable representing an object, holds the memory location of the object.
(Draw a picture.)
Note: For now you only need to understand variables for primitive types.
- A variable is a name for a memory location used to hold a value of a particular data
- You must say what type of variable it is before using it (declaration).
- You can only assign values to a variable that are compatible with
its declared type.
- Variables can either represent primitive types or objects.
- Variable names of primitive types refer directly to the value. (Draw
- Variable names of objects refer to a reference to the object.