Variable:
- Variable is a named memory location used to store data temporarily.
- During program execution we can modify that data
Limitation of Variable:
- It can only store single value at a time.
- It means , always it returns latest modified value.
How can a variable be created?
- A variable can be created by using data type.
- As we have two types of data types we can create two types of variables.
1.Primitive Variable
2.Referenced variable - The difference between primitive and referenced variable is "primitive variable stores data directly , where referenced variables stores reference/address of object, not direct values"
- package com.instanceofjava;
- class Example
- {
- int x=10;
- int y=20;
- }
- package instanceofjava;
- class Test
- {
- public int get(){
- return 10:
- }
- public static void main(String [] args){
- //primitive variables
- int s=50;
- int t=get();
- //reference variables
- String str="a";
- String str1=new String("a");
- Example e= new Example();
- }
- }
What is an object?
- Technically object means collection of all non static variables and methods memory locations.
- Object is created using new keyword
Defining a variable:
- Variable creation with value is called defining a variable
- <Accessibility modifier><Modifier><Data type><Variable name>=<Value> ;
- package instanceofjava;
- public class Demo{
- public static void main(String[] args){
- public static int x=10;
- public static Example e= new Example();
- }
- }
Declaring a variable:
- Variable creation without value is called declaring a variable.
- <Accessibility modifier><Modifier><Data type><Variable name>;
- package instanceofjava;
- public class Demo{
- public static void main(String[] args){
- public static int a;
- public static Example e;
- }
- }
Initializing/Assigning a variable:
- Storing a value in a variable at the time of its creation is called initializing a variable.
- Storing a value in a variable after its creation is called assigning a value to a variable.
- <Variable_Name>=<Value>;
- package instanceofjava;
- public class Demo{
- public static void main(String[] args){
- //declaring a variable
- int a;
- int x;
- //assigning a variable
- a=20;
- x=10;
- // re initialization/ re assignment
- a=30;
- x=20;
- }
- }
Calling a variable:
- Reading a value from a variable is called calling a variable.
- package instanceofjava;
- public class Demo{
- public static void main(String[] args){
- int x=10;
- //calling x variable for printing
- System.out.println(x);
- // calling x variable for initializing y
- int y=x;
- }
- }
Types of Variables:
- Local
- Static
- Non static
- Final
- Transient
- Volatile
1.Local Variables:
- The variables created inside a method or block are called local variables.
Rules:
- While working with local variables , we must follow below 3 rules.
Rule #1:
- Local variables can not be accessed from another method. Because its scope is restricted only within its method.
- Also we can not guarantee that variable creation , because that method may or may not be called. It leads to compile time Exception : can not find symbol.
- package com.instanceofjava;
- class test
- {
- public void show(){
- System.out.println("a:"+a); Compile time error: can not find symbol a.
- }
- public static void main(String [] args){
- int a=10;
- System.out.println("a:"+a);
- }
- }
Rule #2:
- Local variable should not be accessed without initialization.
Example program:
- package com.instanceofjava;
- class test
- {
- public static void main(String [] args){
- int a=10;
- int b;
- System.out.println("a:"+a);
- System.out.println("b:"+b);//Error
- // Above statement throws Compile time Error: variable b might not have been initialized
- b=20;
- System.out.println("b:"+b);
- }
- }
Rule #3:
- Local variable must be accessed only after its creation statement. because method execution is sequential execution from top to bottom. If we access before its creation statement it leads compile time Error : Can not find symbol.
Example program:
- package com.instanceofjava;
- class test
- {
- static void show(){
- System.out.println("a:"+a);// Compile time Error
- int a;
- System.out.println("a:"+a); // Compile time Error
- a=10
- System.out.println("a:"+a);
- }
- }
2. Static variables :
- Static variables gets life when class is loaded.
- It is destroyed either if class is unloaded from JVM or JVM is destroyed.
- Its scope is throughout the class it means where class is available there static variable is available provided it is public.
Example program:
Example program:
Example program:
Example program:
Example program:
You might like:
Top 15 Garbage Collection Interview Questions
Top 50 Java Questions everybody should know
Top 25 Programs asked in interviews
Top 25 Core Java Interview Questions
- package com.instanceofjava;
- class test
- {
- static int a=10;
- public static void show(){
- System.out.println("a:"+a);
- }
- public static void main(String [] args){
- System.out.println("a:"+a);
- }
- }
3. Non Static variables:
- Non static variable gets life when object is created. It is destroyed when object is destroyed.
- Object is destroyed when it is unreferenced.
- Its scope is the scope of the object, object is available only if its referenced variable is available
Example program:
- package com.instanceofjava;
- class test
- {
- int a=10;
- public void show(){
- test t= new test();
- System.out.println("a:"+t.a);
- }
- public static void main(String [] args){
- test t= new test();
- System.out.println("a:"+t.a);
- }
- }
4.Final variables:
- The class level or local variable that has final keyword in its definition is called final variable.
Rule:
- Once it is initialized by developer its value can not be changed. If we try to change its value it leads to Compile tile error.
Example program:
- package com.instanceofjava;
- class test
- {
- static final int a=10;
- static final int b=20;
- public static void main(String [] args){
- a=20;//Error : variable might be already have been assigned
- b=30; //Error : variable might be already have been assigned
- }
- }
5. Transient variable:
- The class level variable that has transient keyword in its definition is called transient variable.
Rule:
- Local variable can not be declared as transient.
- It leads to Compile time error: Illegal start expression
Example program:
- package com.instanceofjava;
- class test
- {
- static transient int a=10;
- static transient int b=20;
- public static void main(String [] args){
- transient int x=10; // compile time error : illegal start expression
- }
- }
- We declare variable as transient to tell to JVM that we do not want to store variable value in a file in object serialization. Since local variable is not part of object , declaring it as transient is illegal.
- Refer IOStreams for more details
6.Volatile Variable:
- The class level variable that has volatile keyword in its definition.
Rule:
- Local variable can not be declared as transient.
- It leads to Compile time error: Illegal start expression
Example program:
- package com.instanceofjava;
- class test
- {
- static volatile int a=10;
- volatile int b=20;
- public static void main(String [] args){
- volatile int x=10; // compile time error : illegal start expression
- }
- }
- We declare variable as volatile to tell to JVM that we do not want to modify variable value concurrently by multiple threads.
- If we declare variable as volatile multiple threads are allowed to change its value in sequence one after another one.
You might like:
Top 15 Garbage Collection Interview Questions
Top 50 Java Questions everybody should know
Top 25 Programs asked in interviews
Top 25 Core Java Interview Questions
No comments