This week's book giveaway is in the Artificial Intelligence forum.
We're giving away four copies of Pragmatic AI and have Noah Gift on-line!
See this thread for details.
Win a copy of Pragmatic AI this week in the Artificial Intelligence forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Adding in a Set  RSS feed

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • 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
  • 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
Java Redhat Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks Dennis
Thanks Gaurav
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!