Variables
A variable is a name given to a memory location. It is the basic unit of storage in a program.
- The value stored in a variable can be changed during program execution.
- A variable is only a name given to a memory location. All the operations done on the variable affect that memory location.
- In Java, all variables must be declared before use.
Types of Variables in Java
Now let us discuss different types of variables which are listed as follows:
- primitive variables
- reference variables
Primitive Variables-: With primitive data types, a variable is a section of memory reserved for a value that uses that data type.
For example by saying long value, 64 bits of memory are reserved for an integer.
The eight primitives defined in Java are int, byte, short, long, float, double, boolean and char.
eg-:
int x;
byte b;
long l;
float f;
Reference Variables-: reference variable is one that refers to the address of another variable. It represents the name of another variable, location, or value. Once you initialize the variable references, that variable will be referred to using the variable name or reference name.
eg-:
Student s=new Student();
s is an object type variable refers to Student.
Based on the position of declaration and behavior all variables are divided into three types.
- Local Variables
- Instance Variables
- Static Variables
Local Variables
Sometimes to meet the temporary requirements of the programmer we can declare variables inside a method or block or constructor such that type variables are called local variables are temporary variables or stack variables are automatic variables.
eg-:
- The local variable will be stored inside stack memory.
- when the local variable will be created while executing block in which be declared it.
- once block execution completes automatically local variable is destroyed hence the scope of local variable is the block in which we declared it.
- local variable also called thread-safe.
- for local variables JVM won't provide default value compulsory we should be initialization explicitly.
- If we are not declaring with any modifier then by default it is default but this rule is applicable only for instance and static variables but not for local variables .
eg-:
public static void main(String... args){
int x;
System.out.print(x);
}
}
//compile time error-: variable x might not have been initialized
- Before we use the variables if we don't use them, we need to initialize them.
2-:
class Test{
public static void main(String... args){
int x;
System.out.print("Hello");
}
}
//Output-: Hello
3-:
class Test{
public static void main(String... args){
int x;
if(args.length>0){
x=20;
}
System.out.print(x);
}
}
//compile time error-: variable x might not have been initialized
- The only applicable modifier for local variable is final by mistake try to apply any other modifier then we will compile time error.
eg-:
class Test{
public static void main(String[] argos){
public int x=20;
private int x=20;
protected int x=20;
static int x=20;
translate int x=20;
volatile int x=20;
// above all are invalid compile time error-: illegal start of expression //
final int x=20;//valid
}
}