JRJC - How To Use Variables

Variables are used to help you keep track of your information while your program is doing other things. They can also be useful in making your program easier to understand. It would be really hard to write a useful program without variables.

There are two categories of variables: Atomics and objects. Atomics are built into the Java language and include int, float and char. Objects are either part of the Java library, are something you made, or come from a third party. Objects are always made of other objects and/or atomics.

Right now all we care about are atomic variables. The most common type of variable used is an integer. Here are three integer variables:

    int a = 5 ;
    int b = 236 ;
    int c = -1 ;
I can modify the variable b with algebraic math:
    b = a + 100 ;
b now contains the value 105: A constant of 100 was added to the variable a which contained the value 5. The variable a was not modified. Chapter 4 of Just Java 1.2 has a good introduction to variables. In Just Java 2 (sixth edition) there is a good discussion of variables in chapter 1.

© 2000 Paul Wheaton