The new keyword is a Java operator that creates the object. The new operator is followed by a call to a constructor, which initializes the new object.
- "new" Keyword: It is responsible to create the object.
- Constructor: It is responsible to initialize the object.
The new operator instantiates a class by allocating memory for a new object and returning a reference to that memory. The new operator also invokes the object constructor.
The new operator returns a reference to the object it created. This reference is usually assigned to a variable of the appropriate type, like:
Point originOne = new Point(23, 94);
The reference returned by the new operator does not have to be assigned to a variable. It can also be used directly in an expression. For example:
int height = new Rectangle().height;