What is reference in Java

Reference data type in java :-

        In java Reference variables are made utilizing characterized constructors of the classes. They are utilized to get to objects. These variables are proclaimed to be of a particular data type that can’t be changed. A few examples of such data types are Employee and Dog.
-> Class objects, and different kind of variables go under reference data type. 
-> Default estimation of any reference variable is invalid.
-> A reference variable can be utilized to allude to any object of the announced sort.
-> Example: myanimal = new Animals(“rabbit”);
-> In java when we declare the variable of class type then it is treated as a reference.
   What is object reference in java.
 
What is reference in Java

Program for Reference Data type :-

program - 1


public class demo
{
 public static void main (String args[])
  {
   demo ab;  //here ab is a reference but it is not initialize by JVM.
            // ab is a variable of demo class, so ab is a reference of demo class.
           // as ab is a reference of demo class it must hold the base address of demo class object.
          // in java JVM manage the memory.
         // ab is initialized by JVM after demo class object is constructed.      
 ab = new demo(); // here also ab is a reference but here ab is initialize by JVM after object constructed.
  System.out.println("i am Asit");
  }
}
Output :-
What is reference in Java
program - 2

public class reference
{
 int data;  // instance variable.
 public static void main(String args[])
 {
  reference dd= null;   // here dd is the reference of reference class.
                                        // dd is a local reference.
  dd= new reference();
  System.out.println(dd.data);
  dd.data++;
  System.out.println(dd.data);
  }
}

  • In java if a programmer is not initialize the instance variable then, At the object creation time JVM initialize the instance variable by default value of data type, the default value of int is 0. In java this is called as auto initialization .
What is reference variable in java.

Output :-
What is reference in Java

program - 3


public class text
{
 int data;
 public static void main (String args[])
  {
  text dd;
  dd = new text();
  dd.data+=5;
  text aa;
  aa= new text();
  aa.data+=10;
  System.out.println(dd.data+"\t"+aa.data);
  swap(dd,aa);
  System.out.println(dd.data+"\t"+aa.data); 
  }
 static void swap(text a,text b)
  {
   int x= a.data;
   a.data= b.data;
   b.data= x;
  }
}
Output :-
What is reference in Java

for more detail follow my Instagram id - https://www.instagram.com/asitmaharana10/

Thank you.