softwarelover
softwarelover

Reputation: 1019

Since all objects are created with "new" in Java, does this mean they are all created on the Heap?

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.

Upvotes: 7

Views: 3025

Answers (4)

roottraveller
roottraveller

Reputation: 8232

In Java, all objects are dynamically allocated on Heap. This is different from C++ where objects can be allocated memory either on Stack or on Heap. In C++, when we allocate abject using new(), the abject is allocated on Heap, otherwise on Stack if not global or static.

In Java, when we only declare a variable of a class type, only a reference is created (memory is not allocated for the object). To allocate memory to an object, we must use new(). So the object is always allocated memory on heap.

Example 1: Give Compile Error.

class Test {
    void show() {
        System.out.println("Test::show() called");
    }
}     
public class Main {
    public static void main(String[] args) {
        Test t; 
        t.show(); // Error here because t is not initialed
    }

Example 2: Allocating memory using new() makes above program work.

class Test {
    void show() {
        System.out.println("Test::show() called");
    }
}      
public class Main {
    public static void main(String[] args) {
        Test t = new Test(); //all objects are dynamically allocated
        t.show(); // No error 
    }
}

Upvotes: 0

Stephen C
Stephen C

Reputation: 718678

All Java objects (i.e. all things with a reference) are allocated in the heap1 from the perspective of the application and the application programmer2. Java does not support explicit allocation of objects on the stack. Object references can be stored both in heap nodes (i.e. class or instance fields) and stack frames (i.e. local variables, etc).

In fact, there are a few ways that first class Java objects can be created in Java that don't involve using the new keyword.

  • The { ... } array initializer syntax can be used in an array declaration without the new keyword.

  • A String literal involves the creation of a String object (at class load time).

  • The boxing conversion will (typically) create a new wrapper object without an explicit new or method call.

  • The reflective newInstance and similar methods create objects without an explicit new.

  • Under the hood, the implementation of Java serialization uses a special method in the Unsafe class to create objects without executing any declared constructor.

  • You can also create Java objects in native code using the JNI / JNA apis.

(There is a strong argument that the last two are "not Java", but they are worth mentioning anyway. And the String literal and auto-boxing cases involve Java code that uses new under the hood.)


1 - There can be more than one heap, though this is transparent to the application.

2 - The latest Hotspot JVMs have an experimental "escape analysis" feature that determines whether objects "escape" from the context in which they are created. Objects that don't escape could be safely allocated on the stack. Once again, this optimization is transparent to the application.

Upvotes: 2

Alnitak
Alnitak

Reputation: 339786

Local primitive types, and local references to object types, both take up "stack" memory, as do they both when passed as parameters to methods.

All objects themselves exist in the equivalent of a "heap".

Upvotes: 4

meirrav
meirrav

Reputation: 771

Yes, the new operator always allocates memory for the object on the heap. Unlike C++, objects in Java cannot be created on the stack.

Upvotes: 8

Related Questions