Where is allocated variable reference, in stack or in the heap?

前端 未结 4 1226
时光说笑
时光说笑 2020-12-01 09:50

I have a question

What happend when I declare a variable inside a method, for example.

void myMethod() {
    Ship myShip = new Ship();
}

Where

4条回答
  •  一个人的身影
    2020-12-01 10:37

    Currently, all Java objects are allocated on the heap. There is talk that Java 7 might do escape analysis and be able to allocate on the stack, but I don't know if the proposal is finalized yet. Here's the RFE.

    Edit: Apparently, it's already in early builds of JDK 7. (The article says it will also be in JDK 6u14, but I can't find confirmation.)

提交回复
热议问题