• 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:

When object created on Heap and when on Stack

 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Which all things are created on Memory Heap and which all things created on Stack.

Whats the basic rule behind this.

Thanks
Gaurav
 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Objects will always be created on heap.There is no other place of creation for objects.Local vaiables are created on stack.
 
ram gaurav
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the answer.

So whats the basic difference beteween stack and heap.
Why we are using 2 places to initialize the objects or variables.

Why we not use only 1.

Thanks
Gaurav
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The stack is used to store the volatile environment of methods. It's allocated when the method is called and released again when the method is left. It holds the variables and methods declared in the method.

A key feature of Java is its garbage-collected heap, which takes care of freeing dynamically allocated memory that is no longer referenced. This frees the programmer from having to keep track of when to free allocated memory, thereby preventing many potential bugs and headaches.
 
ram gaurav
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your clarification.

There is one question ,that, we have:



Can you please tell me that where str and temp will be created.
Either on Heap or Stack.

Thanks
Gaurav
 
Nandini Bhaduri
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Both str and temp will be created on the stack if they are declared in a method, otherwise on the heap.
 
ram gaurav
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But in Javaranch i have read that String literals are never been garbage collected , then hows that possible that they are defined on heap and not been garbage collected.
 
Nandini Bhaduri
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
str and temp are references to strings and not the string itself. "ram" is stored in the literal pool.
 
ram gaurav
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot for clarification.
 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. String str = "ram";
is not same as
2. String temp = new String("ram");

1. will not create any object on the heap it will store this in the literal pool if it is not already present if it is already present then it will not be created again insted the refrence is reused
in this case there is one refrence and one object in the literal pool.
irrespective of where it is written in the class.

But in

2. first it always create a new object on the heap area and then it will check in the literal pool if it is present then it wont add if not a object will be added to literal pool to. so in this case we HAVE ONE REFRENCE AND TWO OBJECTS BUT THE REFRENCE WILL BE POINTING TO HEAP OBJECT.

Thanks
 
Nandini Bhaduri
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the second case two objects will be created. First one associated with the "ram" literal and then a second one which is a clone of the first. Both objects are stored on the heap but the first is immediately released again. A reference to the second one is assigned to temp.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would recommend that when you start to study the stack and the heap, you leave String objects out of the mix - they are a special case! Get the concepts of how the stack and heap work down very clearly first. Then, and only then, you can look at the String constant pool (which, by the way is part of the heap.)

Also, on the 5.0 exam we really tried to minimize the importance of the String constant pool - some of you might get one question that relates to this pool - but most of you probably won't get any - so I'd advise that you focus more on the topics that are tricky AND that get a lot of coverage on the exam. For example: threads, GC (for non-String objects), generics, paths, serialization, collections, inner classes.

hth,

Bert
 
reply
    Bookmark Topic Watch Topic
  • New Topic