Java Variables and Constants

Java Variables
Variables are are named memory locations used to store data in a program. Variables are used to store data whose value may change during program execution. For example, you can write the code shown below to compute the area for different pre-declared radii: // Computing the first area using the first radius radius = 2.0; area = radius * radius * 3.14159; System.out.println("The area is " + area + " for radius " + radius);

// Computing the second area using the second radius radius = 4.0; area = radius * radius * 3.14159; System.out.println("The area is " + area + " for radius " + radius);

Variable Declaration
Variables are used for representing data of a certain type. In order to use a variable, you need to declare it first, declaration is done by telling the compiler the name of the variable as well as what type of data it represents or is supposed to store. This is called a variable declaration, declaring a variable tells the compiler to allocate appropriate memory space for the variable based on the specified data type.

Syntax for declaring a variable: datatype variableName; Examples of variable declarations: int x;              // Declare x to be an integer variable; double radius;      // Declare radius to be a double variable; double interestRate; // Declare interestRate to be a double variable; char a;             // Declare a to be a character variable;

The examples use the data types int, double, and char. Later you will be introduced to additional data types, such as byte, short, long, float, and boolean.

If variables are of the same type, they can be declared together, as follows: datatype variable1, variable2, ..., variablen; Variables of the same data type can be declared on the same line and separated by commas (this should before the semi-colon).

Example: int i, j, k; // Declare i, j, and k as int variables

Note By convention, it is advisable that variable names are declared in lowercase. If a name consists of several words, concatenate all of them and capitalize the first letter of each word except the first letter in the variable name. Example:

double interestRate;

Java Constants
A constant is a named memory location whose value does not change during program execution. While the value of a variable may change during program execution, the value of a constant represents permanent data that never changes. In our AreaCycle sample program below, PI is declared as a constant. If it is used frequently in the program, the programmer  does not have to keep on typing 3.14159 every time the value needs to be used; instead, a  constant declaration for the value can be defined. Syntax for declaring a constant: final datatype CONSTANTNAME = VALUE; A constant is initialized at the point of declaration (must be declared and initialized within the same statement). The word final is a Java reserved word, it means that the constant cannot be changed. Example: // AreaCycle .java: Compute the area of a circle public class AreaCycle { /** Main method */ public static void main(String[] args) { final double PI = 3.14159; // Declare a constant

// Assign a radius double radius = 20; // Compute area double area = radius * radius * PI;

// Display results System.out.println("The area for the circle of radius " +      radius + " is " + area); } }

By convention, Java constants are named in uppercase: use <font color= PI, and not pi or Pi. Advantages of using constants:
 * 1) You don't have to repeatedly type the same value;
 * 2) The value can be changed from a single location if need be;
 * 3) A descriptive name for a constant makes the program easy to read and understand.