• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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: 79177
377
  • 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.
 
On my planet I'm considered quite beautiful. Thanks to the poetry in this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic