In this tutorial, I will be showing you what variables are and how they work in Java.
Variables are simply a representation of a value. Like in basic arithmetic, you can have x = 1. But in Java, the equal sign = is actually not an equal sign; it is an assignment operator. What that means is that the value to the left of the = is transferred to the variable on the right. Not visa versa.
In Java, I like to also think of variables as containers. That’s because variables can “hold” or “contain” more than just numbers. Variables can hold integers, booleans, objects, strings, characters, etc.
So in Java, variables are used to store values to be used later in a program. They are called variables because their values can be changed. For example, in the following code, radius is initially assigned 1.0 and then changed to 2.0, and the area is set to 3.14 and then reset to 12.56
public class circleArea { public static void main(String []args) { // Declare Variables double radius; double area; // Compute the area of a circle radius = 1.0; area = radius * radius * 3.14; System.out.println("The area is " + area + " for radius " + radius); // Re-Compute the area of a circle radius = 2.0; area = radius * radius * 3.14; System.out.println("The area is " + area + " for radius " + radius); } }
Console Output:
Variables can also represent different types of data. Notice that in order to make & use a variable in Java, you first declare the variable’s type and then declare the variable’s name. The variable’s type tells the java compiler how much room in the memory to designate, assign, or allocate so that the data of that type can fit in the memory.
In other words, different types of variables need different amounts of space to store different types of data; that’s why you need to first tell java what type of variable you are making.
The syntax for declaring a variable is:
datatype variableName;
Here are some examples of variable declarations:
int count; // Declare count to only contain integers double radius; // Declare radius to only contain doubles boolean isEmpty;// Declare isEmpty to only contain true/false
These examples use the data type int and double. Later you will be introduced to additional data types, such as byte, short, long, float, float, char, and boolean.
If variables are of the same type, they can be declared together, as follows:
datatype variable1, variable2, ..., variableN;
The variables are separated by commas. For example,
int a, b, c; // Declaring a, b, and c as int variables
Variables often have initial values. You can declare a variable and initialize it in one step. For example:
int count = 1;
is the same as:
int count; count = 1;
You can also use a shorthand form to declare and initialize variables of the same type together. For example:
int i = 1, j = 2;
Note:
- A variable must be declared before it can be assigned a value. A variable declared in a method must be assigned a value before it can be used.
- Whenever possible, declare a variable and assign its initial value in one step. This will make the program easy to read and avoid programming errors.
- You can reassign the values of variables. But if you want to prevent the reassignment of a variable, use the
final
keyword before the variable type & name ie.final int aVar