• 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

Adding in a Set

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

At (2) the size of set remains 2 even though we have removed i2 from the set.Why???
Thanks in advance.
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On line 3, an Integer object is created with a value of 1, and a reference to that object is assigned to the variable i2.
On line 7, a new Integer object is created with a value of 47, and its reference is assigned to the variable i2.
When the call is made to remove i2 from the set, the contents of the set are unchanged because i2 no longer points to an element in the set.
 
Ranch Hand
Posts: 47
Redhat Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
first that is the basis of this program you must to know that Integer is immutable

now you can see figure when 47 value pass it does not go to object that contains 1 value , it create new block of 47 means object in memory same as String work.
now when you used i2 in remove(i2) it refer to new object that contains value 47 but set has not 47 so it return null and nothing is removed from set .

i think this picture will help you.
answer.png
[Thumbnail for answer.png]
 
ishani sharma
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Dennis
Thanks Gaurav
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic