0

can any one makes me understand? how does reference object work in java from below code i have object r1 and reference object r2, Rectangle r1 = new Rectangle(); Rectangle r2 = r1; when i print both r1 and r2 the output is some thing like this Rectangle@17f7bec4(output same for both). ok i understand this is memory address right? if i print below 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);

the output of above code is :

Value of R1's Length : 20

Value of R2's Length : 20

i can not understand this output why both have 20 value ? if this is because of referencing memory then when i use below code

  Rectangle r1 = new Rectangle();
  Rectangle r2 = r1;
  r2 = null;

 System.out.println("R1 : " + r1);
 System.out.println("R2 : " + r2);

the output of both objects:

R1 : Rectangle@17f7bec4

R2 : null

why r1 does not have null value ? this is the point which is confusing me below is running 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);

  }
}

3 Answers 3

2

Your object is created in heap and it has an address.

Rectangle r1 = new Rectangle();
// r1 is pointing to that address
Rectangle r2 = r1;
// now r2 is pointing to the same address (same object) with r1
r2 = null;
// now r2 is null, but the object is still in the heap.
// r2 doesn't point to that object anymore.

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

// now r2 is null, but the object is still in the heap. ok! but when i assigned 20 to r2 (r2.length = 20; ) then why both have same values ? object is still in the heap
You assigned r2 to null after setting object's length to 20
2

r1 and r2 are references to objects. In first case they both point to the same object, in second case r2 points to null. In your code there is only one not-null object and two references.


Comments to your code

Rectangle r1 = new Rectangle();    //object creation, make r1 point to created object
Rectangle r2 = r1;                 //r2 now points to the same object, that was created a line above

r1.length = 50;    //change lenght of object r1 points to
r2.length = 20;    //change lenght of object r2 points to (the same object as r1 points to)

r2 = null; //make r2 point to null

Comments

1

It works the same way for any language.

Rectangle r1 = new Rectangle(); //r1 refers to this new Rectangle
Rectangle r2 = r1; //now r2 refers to the same Rectangle r1 refers to
r2 = null; //now is referring to null

You could set r1 to null and it will have the same behavior.

If you change r2 to final it will not allow it to be changed.

Rectangle r1 = new Rectangle();
final Rectangle r2 = r1; //now r2 will be forever referring to the Rectangle above even if you change r1 to null

check it out if you change r1 to null r2 will still point to the Rectangle... when you tell r2=r1... r2 is NOT pointing to r1, r2 is actually pointing to the Rectangle itself!

Rectangle r1 = new Rectangle(2, 2);
final Rectangle r2 = r1; // now r2 will be forever referring to the
             // Rectangle above even if you change r1 to
             // null
r1 = null;
System.out.print(r2.getWidth());

output width is still 2 and no null error.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.