• 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:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Please explain auto boxing behavior

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
// PLEASE EXPLAIN FOLLOWING PROGRAM'S BEHAVIOUR
---->Please explain me how am I getting this OUTPUT ?
---->When I add i1 and i2 into LinkedHashSet with values 20 and 25 respectively
----> I am reassigning i2 as 33 and when I remove i2 it removes i2 with value 33 BUT i2 with value 25 remains in set .At this point only I have got confused.









 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are assigning i2 to 33 after inserting i2 into the LinkedHashSet. Now when you try to remove i2, you are actually trying to remove a 33 from the Set which doesn't exist in the Set.
It is similar to,

hs.add(20)
hs.add(25)

and then
hs.remove(33) // 33 doesn't exist in hs. So nothing is removed!!3

When you insert i2 into the set, the set hs stores 25 in it. So when you reassign i2 to 33, the value in hs is unchanged and is 25 only.
 
dhaval dethe
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your explanation.
I read javadoc for remove method which returns Boolean value as 'false' which means value of i2 =33 has not been added to set so remove() method returns false.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!