A variable stores a value of a particular type. A variable has a name , a type, and a value associated with it. In Java, variables can only store values of primitive data types and references to objects. Variables that store references to objects are called reference variables . Declaring and Initializing VariablesVariable declarations are used to specify the type and the name of variables. This implicitly determines their memory allocation and the values that can be stored in them. We show some examples of declaring variables that can store primitive values: char a, b, c; // a, b and c are character variables. double area; // area is a floating-point variable. boolean flag; // flag is a boolean variable. The first declaration above is equivalent to the following three declarations: char a; char b; char c; A declaration can also include initialization code to specify an appropriate initial value for the variable: int i = 10, // i is an int variable with initial value 10. j = 101; // j is an int variable with initial value 101. long big = 2147483648L; // big is a long variable with specified initial value. Object Reference VariablesAn object reference is a value that denotes an object in Java. Such reference values can be stored in variables and used to manipulate the object denoted by the reference value. A variable declaration that specifies a reference type (i.e., a class, an array, or an interface name) declares an object reference variable. Analogous to the declaration of variables of primitive data types, the simplest form of reference variable declaration only specifies the name and the reference type. The declaration determines what objects a reference variable can denote. Before we can use a reference variable to manipulate an object, it must be declared and initialized with the reference value of the object. Pizza yummyPizza; // Variable yummyPizza can reference objects of class Pizza. Hamburger bigOne, // Variable bigOne can reference objects of class Hamburger, smallOne; // and so can variable smallOne. It is important to note that the declarations above do not create any objects of class Pizza or Hamburger . The declarations only create variables that can store references to objects of these classes. A declaration can also include an initializer to create an object whose reference can be assigned to the reference variable: Pizza yummyPizza = new Pizza("Hot&Spicy"); // Declaration with initializer. The reference variable yummyPizza can reference objects of class Pizza . The keyword new , together with the constructor call Pizza("Hot&Spicy") , creates an object of class Pizza . The reference to this object is assigned to the variable yummyPizza . The newly created object of class Pizza can now be manipulated through the reference stored in this variable. Initializers for initializing fields in objects, classes, and interfaces are discussed in Section 8.2. Reference variables for arrays are discussed in Section 4.1. Lifetime of VariablesLifetime of a variable, that is, the time a variable is accessible during execution, is determined by the context in which it is declared. We distinguish between lifetime of variables in three contexts:
|