Variables and Constants

Variables

We have simple variables (also called "primitive types" in Java) and more complex variables that can call their own functions. In Java, as with most programming languages, variables must be "declared" before you can use them.

Primitive Types:

There are other primitive types (byte, float, long, and short), but those listed above are the ones we will use most often.

More complex variable type (sometimes called "class types" or "reference types")

String name;	//name is declared
name="Bonnie";  //a string may hold a set of characters, including spaces.

name now has a set of functions (methods) it can use to work with its value.
Example: name.length() will tell us how many characters are in the string.

Constants

Constants are like variables, in that they are used to refer to values, but unlike variables, their values may not change.

Example:

static final PI = 3.14159; static final TAX_RATE=0.045;

Note that by convention, constants are always capitalized. You will lose points if you put constants in lower case.

Constants must be declared at the class level and may not be declared inside functions.