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