java object

i want to know that when we create the object of a class it is created on heap or in stack?
[98 byte] By [ashok02a] at [2007-11-26 15:36:14]
# 1
All objects are created on the heap.
Drossa at 2007-7-8 21:53:50 > top of Java-index,Java Essentials,New To Java...
# 2
> All objects are created on the heap.For the time being.
Drossa at 2007-7-8 21:53:50 > top of Java-index,Java Essentials,New To Java...
# 3

> i want to know that when we create the object of a

> class it is created on heap or in stack?

Stack memory is associated with method calls. It's allocated when a method is called and released again when the method finishes. So if objects were allocated on the stack they would exist only during a method call, which would be very limiting.

So for objects to have longer lifetimes they cannot be allocated on the stack, but note that some objects actually exists during a metod call only and such object could be allocated on the stack. In fact this is a proposed optimization we may see in version 7.

So in the future not all objects may be allocated on the heap any more. The advantage of allocating objects on the stack is speed. The time-consuming heap/garbage collection cycle is avoided.

Drossa at 2007-7-8 21:53:50 > top of Java-index,Java Essentials,New To Java...
# 4

> i want to know that when we create the object of a

> class it is created on heap or in stack?

Java as a programing language does not offer any way to explicitly allocate an object on the stack, but this fact doesn't prevent JVMs (in Java 6) from still using stack allocation where appropriate, with a technique called escape analysis.

So the object you create to be heap-allocated, might actually be optimized by the JVM to be stack-allocated.

karma-9a at 2007-7-8 21:53:50 > top of Java-index,Java Essentials,New To Java...
# 5

> Java as a programing language does not offer

> any way to explicitly allocate an object on the

> stack, but this fact doesn't prevent JVMs (in

> Java 6) from still using stack allocation where

> appropriate, with a technique called escape

> analysis.

Are you sure escape analysis was introduced in version 6 already? I wouldn't be happier but have you firm information that it actually was?

karma-9a at 2007-7-8 21:53:50 > top of Java-index,Java Essentials,New To Java...
# 6

> Are you sure escape analysis was introduced in

> version 6 already? I wouldn't be happier but have you

> firm information that it actually was?

Good question. Seems that it was at least partially [url=http://java.sun.com/javase/6/webnotes/features.html]implemented[/url], with a corresponding RFE of [url=http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6339956]6339956[/url] ...

karma-9a at 2007-7-8 21:53:50 > top of Java-index,Java Essentials,New To Java...
# 7

> Good question. Seems that it was at least partially

> [url=http://java.sun.com/javase/6/webnotes/features.ht

> ml]implemented[/url], with a corresponding RFE of

> [url=http://bugs.sun.com/bugdatabase/view_bug.do?bug_i

> d=6339956]6339956[/url] ...

From what I've been able to find out escape analysis is implemented in version 6 but still hasn't been used to optimize object creations (but it has been used to optimize some other aspects of object handling so programs invoking escape analysis still may run faster).

karma-9a at 2007-7-8 21:53:50 > top of Java-index,Java Essentials,New To Java...