wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes containers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "containers" Watch "containers" New topic
Author

containers

Ar Yasoda
Greenhorn

Joined: Apr 04, 2007
Posts: 9
if i declared,

set<String>s=new HashSet<String>()

and then if i say..

s.add("hello");

where will be and how the memory for hello be created.Is it in heap?
Ricky Clarkson
Ranch Hand

Joined: Jul 27, 2006
Posts: 131
All objects are on the heap. "hello" is a String, Strings are objects.
Ar Yasoda
Greenhorn

Joined: Apr 04, 2007
Posts: 9
Thanks for the reply.
And how can the memory for s which is the instance of set get expanded when ever we are adding the objects into the container.

regards,
yasoda.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
Go into your Java installation file, and explore it until you find a file called src.zip. Unzip that file, go into the "java" then the "util" folders and find "HashSet.java." Open it. You will find that HashSet uses a HashMap, so you open HashMap, and there you find that HashMap uses an array of Entries. I haven't read it properly, but shall leave that to you. It appears to double its capacity whenever the array is more than 75% full, and to create a new Entry[] array whenever its capacity changes/
Ricky Clarkson
Ranch Hand

Joined: Jul 27, 2006
Posts: 131
Generally, memory doesn't get expanded, it gets reallocated and copied. The choice about when to do that is hard to make, so the standard collections do it for us, choosing sensible times. For example, an empty ArrayList may have a 100 element array in it, and when you fill that, it'll allocate a 200 element array, and copy all the data across.

I don't know the exact strategy used, and I don't care, but it will be a variant on the above.
 
wood burning stoves
 
subject: containers
 
Similar Threads
mock exam question
Different Output using setContentType
jsp syntax
JSP Tags - Attributes passed returns NULL [SOS Please help]
KSoap2 question, please reply