5.6. Variables

A variable can hold one item of a given type. If you store a new item into a variable, it replaces the old item.

Creating a variable

If T is a type and v is a name, then statement
  T v;
creates a new variable called v that can hold one item of type T. For example,
  long x;
creates a new variable called x of type long and
  double frog;
creates a new variable called frog of type double. You can create several variables of the same type together, as in
  double width, length, height;

No automatic initialization

When a variable of one of the numeric types, such as double or int, is created in this way, no particular value is stored in it. The variable is uninitialized.

You are required to initialize a variable before you use it.


Variable names

For a variable name, use a name that starts with a letter and can have letters, digits and the underscore character. For example, x, show_me and index12 are acceptable variable names. Do not put a space in a variable name.

The case of letters matters. For example, Size and size are considered different variable names.


Assignment

Statement v = E computes the value of expression E then stores that value into variable v. The value of the expression is the value that was just stored into v.

Creating initialized variables

When you create a variable you can also give it an initial value. Statement
  int r = 1;
is equivalent to the two statements
  int r;
  r = 1;
Creating and initializing several variables of the same type is easy:
  long w = 0, n = 1;

Increment and decrement operators

It is very common to find that you want to add 1 to a variable or subtract 1 from a variable (changing the value of the variable). Of course,
  n = n + 1;
will change n to have a value one larger than its former value. But you can also write
  n++;
to mean the same thing. Similarly, statement
  n--;
is equivalent to statement
  n = n - 1;

Operator assignment

Statement
  x += k;
is equivalent to
  x = x + k;
You can use any binary operator (+, −, *, etc.) with =. For example, statement
  x *= n + 1;
has the same effect as statement
  x = x * (n+1);

Assignment uses current values of variables

Statement
  x = y;
does not make x permanently equal to y. It only says to put the current value of y into x. For exaple,
  y = 0;
  x = y;
  y = 1;
ends with x = 0 and y = 1.


Some uses for variables

Using variables to avoid computing something twice

Sometimes you want to use the value of an expression in two places, but only compute the expression once. That is just a matter of storing the result of the expression in a variable so the the program remembers it. See the next item.

Using variables to make programs more readable

Some expressions are long and complicated. Using variables can simplify them. For example, imagine computing the two solutions for x to equation ax2 + bx + c = 0. The following does the job.
  double disc  = b*b - 4*a*c;
  double s     = Math.sqrt(disc);
  double twoa  = 2*a;
  double soln1 = (-b + s)/twoa;
  double soln2 = (-b - s)/twoa;
Notice that s is computed once but used twice. Variable disc is just used to shorten an expression. (You also might want to test whether disc ≥ 0, since otherwise the expression sqrt(disc) causes an error.


Exercises

  1. Is there a default value to which variables of type int are initially set? Answer

  2. Rewrite the following so that it does not compute Math.max(x,y) twice.

      int x = Math.max(x,y) * Math.max(x,y) + 1;
    
    Answer

  3. Is the following sequence of statements allowed?

      int d = 40;
      int d = d + 1;
    
    Answer

  4. Is the cat an acceptable name for a variable? Answer

  5. If you use a variable that you have not created, will the compiler create it automatically for you? Answer

  6. What is the value of variable x after doing the following statement?

      double x = 1/3;
    
    Answer