title: how does reference object work in java
body: <p>can any one makes me understand? how does reference object work in java from below code i have object <code>r1</code> and reference object <code>r2</code>, 
  <code>Rectangle r1 = new Rectangle(); Rectangle r2 = r1;</code>
  when i print both <code>r1</code> and <code>r2</code> the output is some thing like this <code>Rectangle@17f7bec4</code>(output same for both). ok i understand this is memory address right? if i print below code</p>
  
  <pre><code>  r1.length = 50;
    r2.length = 20;
  
    System.out.println("Value of R1's Length : " + r1.length);
    System.out.println("Value of R2's Length : " + r2.length);
  </code></pre>
  
  <p>the output of above code is :</p>
  
  <p><strong>Value of R1's Length : 20</strong> </p>
  
  <p><strong>Value of R2's Length : 20</strong></p>
  
  <p>i can not understand this output why both have <code>20</code> value ?
  if this is because of referencing memory then when i use below code</p>
  
  <pre><code>  Rectangle r1 = new Rectangle();
    Rectangle r2 = r1;
    r2 = null;
  
   System.out.println("R1 : " + r1);
   System.out.println("R2 : " + r2);
  </code></pre>
  
  <p>the output of both objects:</p>
  
  <p><strong>R1 : <code>Rectangle@17f7bec4</code></strong></p>
  
  <p><strong>R2 : null</strong></p>
  
  <p>why r1 does not have <code>null</code> value ? this is the <strong>point</strong> which is confusing me
  below is running code..</p>
  
  <pre><code>class Rectangle {
    double length;
    double breadth;
  }
  
  class RectangleDemo {
    public static void main(String args[]) {
  
    Rectangle r1 = new Rectangle();
    Rectangle r2 = r1;
  
  r1.length = 50;
  r2.length = 20;
  
  
    System.out.println("R1 : " + r1);
    System.out.println("Value of R1's Length : " + r1.length);
    System.out.println("Value of R2's Length : " + r2.length);
  r2 = null;
    System.out.println("R2 : " + r2);
  
    }
  }
  </code></pre>