• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

containers

 
Ar Yasoda
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All objects are on the heap. "hello" is a String, Strings are objects.
 
Ar Yasoda
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48381
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic