• Post Reply Bookmark Topic Watch Topic
  • New Topic

Object creation in java  RSS feed

 
Gopi Nese
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In java,Who creates the object and where exactly it goes to store.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

You do not need the answer to either of those questions.
The JVM.
If it is a field, usually on the heap, or if it is a local variable, usually on the stack. (I think).
 
vineet dhar
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gopi Nese wrote: In java,Who creates the object and where exactly it goes to store.


In java objects are created with the use of "new" operator...As soon as the compiler finds "new" operator, it allocates memory for that instance...and we say an object has been created..it is stored in the heap...and the object's reference gets stored on the stack...All of this is handled by the java virtual machine
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:if it is a local variable, usually on the stack. (I think).

Not really, only references will be stored in stack . objects are always will be created in heap .

<edit>corrected typo</edit>
 
vineet dhar
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seetharaman Venkatasamy wrote:
Campbell Ritchie wrote:if it is a local variable, usually on the stack. (I think).

Not really, only references will be stored in heap . objects are always will be created in heap .



I am not sure but reference are stored on the stack..the instance or the memory to which they are pointing are stored on the heap...And as ritchie said, local variables are stored on the stack always....
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Objects will usually be created on the heap (the compiler sometimes does some advanced stuff however, and I think will sometimes create them on the stack if it's able to).

References and primitives will be stored on the heap, if they are stored in member variables (of an object that's stored on the heap), or on the stack if they are local variables.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seetharaman Venkatasamy wrote:. . . objects are always will be created in heap . . .
Yes, I think you are correct and I was mistaken.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Objects normally are created on the heap, but as Stephan already hinted, things have become a little more complicated since escape analysis was added to the JVM.

The idea of escape analysis is to check if in a method objects are created that don't "escape" from the method - temporary objects, that are only used inside the method, and are eligible for garbage collection as soon as the method returns. It's a good idea to allocate these objects on the stack instead of on the heap, because then deallocating them will cost nothing - the stack is unwound when the method returns, cleaning up any objects allocated there. The garbage collector, which cleans up unused objects on the heap, doesn't have to deal with these temporary objects. It's a nice performance optimization, which was added in one of the Java 6 updates (I think Java 6 update 14, although it was not enabled by default in that version).

Note that this is definitely not a beginner's topic. You don't need to know these things, unless you want to be an expert in the inner workings of the JVM.

For the general idea, just remember that objects created with new are always on the heap. The stack is only for storing local variables (references and primitive variables) inside a method, the return address etc. - see call stack.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wow, great knowledge sharing. thanks Jesper
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!