Explain the usage of the new operator in Java.
Advertisements
Question
Explain the usage of the new operator in Java.
Explain
Advertisements
Solution
The new operator in Java is used to create an object or instance of a class dynamically during program execution. It allocates memory for the object and invokes the constructor of the class to initialise the object.
- Object Creation: The
newoperator is used to create an instance of a class in Java. - Memory Allocation: It dynamically allocates memory for the newly created object during program execution.
- Constructor Invocation: It automatically calls the constructor of the class to initialise the object.
- Reference Assignment: It returns a reference to the newly created object, which can be stored in a reference variable.
- Array Creation: The
newoperator is also used to create arrays in Java.
shaalaa.com
Is there an error in this question or solution?
