Variables and Constants

In Java we deal with lots and lots of data which needs to be stored at some place from where it can be manipulated. Such containers of data are referred to as variables and constants.

What are variables?

A variable is a location in memory, which is assigned a name and is used to store data temporarily i.e. a variable provides a named storage that our programs can manipulate. Each variable in Java has a specific type and a specific size. Variable means changing, so when we declare any variable, we can change the values of that variable during the execution of the program.

For example: we want to calculate the area of a square, we can write it as:

Area=s*s;

Now, during execution we can find the area of various squares with variable sides as each time the variable s will store a new value of the side of the square and compute the area. Each time the value of the variable is store at the same location in the memory, the previous value is replaced with the new one.

Declaring a variable

We cannot use a variable directly in a Java program. Before using it in our program, we need to declare it. To declare a variable the following syntax is used:

Datatype variable_name [=value] [, variable_name [=value] …..];

Where datatype refers to the type of the variable and variable_name is an identifier i.e. the name of that variable.

For example:

int a, b, c;                    // declares 3 integers a, b and c

int a=10, b=20;            // declares 2 integers a and b and initializes their values as well

float percent;              // declares a variable percent as floating type

char name;                  // declares a character variable

What are constants?

Constants are the data elements whose values remain unchanged during the course of a program. To declare a constant we use the final keyword. Once a constant has been declared, we have to initialize it along with its declaration as the value cannot be assigned to it later in the program. The naming of constants follows the same conventions as that of a variable. However, UPPERCASE letters should be used in order to declare constants in order to distinguish them from regular variables.

For example: final float PI=3.14;

The final keyword ensures that the value of the constant PI remains unchanged throughout the execution of the program.

Java Variables and Constants Naming Conventions

Each programming language has a set of rules and conventions for the kind of names. These conventions can be used in declaring various elements in our program. Java is no different and has the following set of rules and conventions for declaring variables and constants:

  • Variables and constants names are case sensitive. For example: int InitialValue is different from int initialValue or int Initialvalue.
  • Subsequent characters of a variable name may be letters, digits, dollar sign ‘$’ or underscore characters ‘_’. However abbreviations can be used but it is preferred to use full words instead as doing so will make the code easier to be read and understood.
  • Variable name must not be a keyword or a reserved word.
  • Variables and constants names cannot have spaces. If a variable name comprises of two words in it, it cannot have space in between them. Either you can use an underscore between the two words or you can capitalise the first character of the second word without giving any space between them. For example:

int Initial value;           // Wrong way

int InitialValue;           // Correct Way 1

int Initial_value;          // Correct way 2