In this post of Intro to Java, we mentioned int minutes would declare an int variable named minutes, while minutes = 1987 initializes the variable with a value of 1987.
Similarly, there is a difference between object declaration and object initialization. Rectangle x; is declaration, while new Rectangle(2, 2, 4, 5) creates the object and the object reference will be saved in x. So x= new Rectangle(2, 2, 4, 5) initializes x.
What if you only declare the object variable? - a special keyword null is stored in the object variable, meaning this variable points to no object.
Keep in mind, only keyword new can create and initialize an object. In the code below, y=x only assign x object's reference to y, it does NOT create a new object, so both x and y are pointing to the same object. x and y are aliases of the same object.
// x and y are aliases of the same object, so if x is changed, y will be changed tooRectangle x = new Rectangle(2, 2, 4, 5)Rectangle y = x;x.setLength(10);System.out.println(y.getLength()); // this will print 10// Compare the above with primitive types:double a = 100.05;double b=a ; // b=100.05a = -1.2; // this will NOT change the value of bSystem.out.println(b); // this will print 100.95