Variables in Java – Types and Examples
As we all know, Java processes information or data in the form of tokens. These tokens are the fundamental unit of a Java program and hence, we can consider them as one of the most important parts of a Java program. Variables are a kind of token, so it’s essential to know how to work with variables in Java. In this blog, you will learn what the word “variable” means with reference to Java, rules to make a correct variable, types of variables and how to declare or initialise them in Java.
What are Java Variables?
Variables in Java, are a name given to a specific location in the memory at which the machine stores your data. They can contain some data which the user gives as input or some constant value too. However, the value that a variable contains can change on different instances according to the operations we perform on it.
- Variables are basically, a specific name given to a memory space where you store your data.
- We can perform different kinds of operations on variables and the machine will manipulate and adjust the data according to the carried out operation. The machine will maintain the data in the same location.
- We need to declare and initialise variables before using them anywhere in a program.
Variables are classified on the basis of scope . - When we use the final keyword with a variable it makes the variable value constant. Then that value remains the same during the course of the program.
Declaration of a Variable in Java
It is highly important to declare a variable when you create it. We declare a variable by specifying its data type.
Syntax:
<data type><space><variable name>;
Example of java variable declaration:
int var;
It’s important to declare a variable because when we create a variable the compiler allocates memory to it. For proper allocation of memory it is mandatory to know the data type of a variable as for different data types the amount of memory space required is different. So, when we specify the data type, the compiler is able to calculate the size of memory required for a specific variable.
Initializing a variable in java
We initialize a variable by assigning a value to it. It is essential to assign a value to a variable because after declaration it can contain some garbage value and that can hamper the output that you expect from a program. In other words, if you don’t initialize the variables, you won’t be able to get the desired result as an output to your program even after having no logical errors.
Example for java variable initializing:
int a; a=0;
There are two types of initialization:
a. Static Initialization of variable in Java
We perform this kind of initialization by assigning a defined constant value to a variable along with it’s declaration or just after it’s declared. We can do this in a single line, along with declaration or we can do it in two separate lines just after declaration.
Example for java static initialization:
double var=0.0; OR double var; var=0.0;
b. Dynamic Initialization of variable in Java
When the compiler assigns the value to a variable during run time, this type of initialization takes place. This takes place when the execution of a program starts. This happens when a variable is initialized with the result of an expression or any other operation.
Example of java Dynamic Initialization:
double var1,var2,res; var1=2.0; var2=2.0; res=var1+var2;
Rules to Name a Variable in Java
a. A variable should always start with a capital or small letter, Underscore or a Dollar sign.
b. A variable can contain any number of characters.
c. Spaces and special characters aren’t allowed in a variable name in Java.
d. Variable names are case sensitive. That is, a variable named “VAR1” is different from a variable named “var1”.
e. Reserved keywords can not be used as variable names.
Types of variables in Java
There are three types of variables in Java: Local Variables, Instance Variables, Static Variables.
a. Local Variables in java
A local variable is the one that is declared in the body of a function or method. It can only be used inside that method and is inaccessible to other functions. This kind of variable is created within the function and is destroyed after the execution of that function. It’s necessary to initialize these variables for proper usage.
Example of java Local Variables:
public class FirstCode { void examplemethod() { int var1=0;//local_variable } }
b. Instance variable in java
These are the variables that are not a part of any method or function. These variables are declared in a class but outside a method. These are created with the objects of a specific class and are destroyed along with them. To access them or refer to them we make use of access specifiers. There’s no need to initialize them before using them as they contain default values.
Example for Java Instance Variable:
public class FirstCode { public int marks;//instance_variable private int salary;//instance_variable(accessible to only FirstCode class). void examplemethod() { int var1=0;//local_variable } }
c. Static Variables in Java
These are the kind of variables that are declared outside a method using static functions. Their value remains constant during the course of the program. Memory is allocated to it only once. Static variables do not belong to a specific object instead, they belong to the class itself and hence they are used to represent the attributes that are shared by all the objects.
Example for Java Static Variable:
public class FirstCode { static String Company name= “FirstCode”;//static variable void examplemethod() { int var1=0;//local_variable } }
Summary
Variables in Java are the most fundamental part of a program so we are expected to know how to work with them. Learning about the kind of variables available to us, how to create them, the rules to name them and the process of initialization and declaration lets us work effortlessly with them. All of which is available to you in this blog.