rectangle r1=new rectangle();

rectangle r2=r1;

whats the working of second line here

Pointer type pointerName = new Object of that type(); Consider the code rectangle r1 = new rectangle(); . This creates a new rectangle object and stores it in the computer's memory. It then creates a new pointer, r1, that points to that memory slot:

r1 ------> [rectangle() created] rectangle r2=r1; basically creates another pointer to point to an object. r2 is the pointer, but the object is the one stored in r1... Now the computer memory looks morel like this:

r1 -----> [rectangle() created] <----- r2

In other words, r1 and r2 point to the same object. Changes made to the object referenced at r2 happen also to the object referenced at r1, because it is one and the same.