Assigning one object reference variable to another object reference variable

Introduction 

 
Starting by taking two variables var1 and var2 of integer type, such as
 
int var1 = 5;
int var2 = var1;
 
Simply, 5 is assigned to var1 and the value of var1 is assigned to var2.
 
Changing the value of var2 as
 
var2 = 10;
 
When you print var1 and var2, you get 5 and 10 as output respectively.
  
But, in case of object reference variables, you may marvel, when assigning one object reference variable to another. To understand, what's the thing I am trying to be pointed to, review the simple java program written below
 
Code:
  1. public class MyJavaClass   
  2. {  
  3.  public static void main(String[] args)   
  4.  {  
  5.   Demo obj = new Demo();  
  6.   obj.a = 30;  
  7.   obj.b = 50;  
  8.   Demo obj1 = obj;  
  9.   System.out.println(obj.add(obj.a, obj.b));  
  10.   System.out.println(obj.add(obj1.a, obj1.b));  
  11.   obj.a = 50;  
  12.   obj.b = 50;  
  13.   System.out.println(obj.add(obj.a, obj.b));  
  14.   System.out.println(obj.add(obj1.a, obj1.b));  
  15.   obj1.a = 10;  
  16.   obj1.b = 20;  
  17.   System.out.println(obj.add(obj.a, obj.b));  
  18.   System.out.println(obj.add(obj1.a, obj1.b));  
  19.   Demo obj2 = new Demo();  
  20.   obj2.a = 5;  
  21.   obj2.b = 6;  
  22.   System.out.println(obj.add(obj2.a, obj2.b));  
  23.   obj2 = obj1;  
  24.   System.out.println(obj.add(obj2.a, obj2.b));  
  25.   obj2.a = 15;  
  26.   obj2.b = 75;  
  27.   System.out.println(obj.add(obj.a, obj.b));  
  28.   System.out.println(obj.add(obj1.a, obj1.b));  
  29.   System.out.println(obj.add(obj2.a, obj2.b));  
  30.  }  
  31. }  
  32. class Demo   
  33. {  
  34.  int a, b;  
  35.  public int add(int a, int b)   
  36.  {  
  37.   return a + b;  
  38.  }  
  39. }   
Output:
 
80 
80
100 
100
30
30
11
30
90 
90
90
 
You can observe that after assigning one object reference variable to another object reference variable. The output of "a+b" is same whether you are using any of object reference variable. This happens, because the assignment of one object reference variable to another didn't create any memory, they will refer to the same object. In other words, any copy of the object is not created, but the copy of the reference is created. For example, obj1 = obj; The above line of code just defines that obj1 is referring to the object, obj is referring. So, when you make changes to object using obj1 will also affect the object, b1 is referring because they are referring to the same object.