• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

containers

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 75724
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
This is my favorite show. And this is my favorite tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic