• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Object creation in java

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In java,Who creates the object and where exactly it goes to store.
 
Marshal
Posts: 80749
486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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....
 
Bartender
Posts: 15741
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 80749
486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
wow, great knowledge sharing. thanks Jesper
 
reply
    Bookmark Topic Watch Topic
  • New Topic