» » Java Variables and Types of Variables


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"

  1. package com.instanceofjava;
  2.  
  3. class Example
  4. {
  5.  
  6.  int x=10;
  7.  int y=20;
  8.  
  9. }


  1. package instanceofjava;
  2. class Test
  3. {
  4.  
  5. public int get(){
  6. return 10:
  7. }
  8.  
  9. public static void main(String [] args){
  10.  
  11. //primitive variables
  12. int s=50;
  13. int t=get(); 
  14.  
  15. //reference variables
  16. String str="a";
  17. String str1=new String("a");
  18. Example e= new Example();

  19. }
  20. }



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> ;

  1. package instanceofjava;
  2. public class Demo{
  3. public static void main(String[] args){
  4.   
  5. public static int x=10;
  6. public static Example e= new Example();

  7. }
  8. }

Declaring a variable:

  • Variable creation without value is called declaring a variable.
  • <Accessibility modifier><Modifier><Data type><Variable name>;


  1. package instanceofjava;
  2. public class Demo{
  3. public static void main(String[] args){
  4.   
  5. public static int a;
  6. public static Example e;

  7. }
  8. }


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>;

  1. package instanceofjava;
  2. public class Demo{
  3. public static void main(String[] args){
  4.   
  5. //declaring a variable
  6.  int a;
  7.  int x;
  8.  
  9. //assigning a variable
  10.  a=20;
  11.  x=10;
  12.  
  13. // re initialization/ re assignment
  14.  a=30;
  15.  x=20;

  16. }
  17. }


Calling a variable:

  • Reading a value from a variable is called calling a variable.


  1. package instanceofjava;
  2. public class Demo{
  3. public static void main(String[] args){
  4.   
  5.  int x=10;
  6.   
  7.  //calling x variable for printing
  8. System.out.println(x);
  9.  
  10. // calling x variable for initializing y
  11.  int y=x;


  12. }
  13. }



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.
 Example program:

  1. package com.instanceofjava;
  2. class test
  3. {
  4.  
  5. public void show(){
  6.  System.out.println("a:"+a); Compile time error: can not find symbol a.
  7. }
  8.  
  9. public static void main(String [] args){
  10.  
  11.  int a=10;
  12.  System.out.println("a:"+a);
  13. }
  14. }


Rule #2:

  • Local variable should not be accessed without initialization.

 Example program:

  1. package com.instanceofjava;
  2. class test
  3. {
  4. public static void main(String [] args){
  5.  
  6.  int a=10;
  7.  int b;
  8.  System.out.println("a:"+a);
  9.  
  10.  System.out.println("b:"+b);//Error
  11.  // Above statement throws Compile time Error: variable b might not have been initialized
  12.  
  13.  b=20;
  14. System.out.println("b:"+b);
  15.  
  16. }
  17. }


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:

  1. package com.instanceofjava;
  2. class test
  3. {
  4.  
  5.  static void show(){
  6.  System.out.println("a:"+a);// Compile time Error
  7.  int a;
  8.  System.out.println("a:"+a); // Compile time Error
  9.   
  10.  a=10
  11.  
  12.  System.out.println("a:"+a);
  13.  
  14. }
  15. }

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:

  1. package com.instanceofjava;
  2. class test
  3. {
  4.  static int a=10;

  5. public static void show(){
  6.  System.out.println("a:"+a);
  7. }
  8.  
  9. public static void main(String [] args){
  10.  
  11.  System.out.println("a:"+a);
  12.  
  13. }
  14. }

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:

  1. package com.instanceofjava;
  2. class test
  3. {
  4.  
  5.  int a=10;

  6. public void show(){
  7.   test t= new test();
  8.  System.out.println("a:"+t.a);
  9. }
  10.  
  11. public static void main(String [] args){
  12.  
  13.   test t= new test();
  14.  System.out.println("a:"+t.a);
  15.  
  16. }
  17. }


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:

  1. package com.instanceofjava;
  2. class test
  3. {
  4.  static final int a=10;
  5. static final int b=20;

  6. public static void main(String [] args){
  7.  
  8.  a=20;//Error : variable might be already have been assigned
  9.  b=30; //Error : variable might be already have been assigned
  10.  
  11. }
  12. }


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:

  1. package com.instanceofjava;
  2. class test
  3. {
  4.  static transient int a=10;
  5. static transient  int b=20;

  6. public static void main(String [] args){
  7.  
  8.  transient int x=10; // compile time error : illegal start expression
  9.  
  10. }
  11. }

  • 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:

  1. package com.instanceofjava;
  2. class test
  3. {
  4.  static volatile int a=10;
  5. volatile int b=20;

  6. public static void main(String [] args){
  7.  
  8.  volatile int x=10; // compile time error : illegal start expression
  9.  
  10. }
  11. }

  • 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

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu