The purpose of this query is to compare one aspect of Java and C++, that has to do with the “new” operator.
Now, I know that in C++ there are two ways to create objects; with or without the “new” operator. In the absence of that operator, space is not allocated in the heap region, whereas, in its presence, space is allocated in the heap region.
What about Java? I notice that the “new” operator is used for creating every object. Even arrays are created with the “new” operator. Does it mean that in Java there is only one place for objects to exist in – that is, the heap region?
Thanks.
Yes, the new operator always allocates memory for the object on the heap. Unlike C++, objects in Java cannot be created on the stack.