• Post Reply Bookmark Topic Watch Topic
  • New Topic

can any body tell me why am i getting out put 211 after removing both i1,i2  RSS feed

 
yaswant kumar kumar
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

iam getting output as 211 first i removed i1, and next i removed i2 but still am getting 211 why is it please tell me
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Integer objects are immutable. Once you assign a different value to i2, it points to a different object. Thus, removing that object from the Set does nothing, because it never was in the Set to begin with. If you comment out line 11 you will see the behavior that you were expecting.
 
yaswant kumar kumar
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:Integer objects are immutable. Once you assign a different value to i2, it points to a different object. Thus, removing that object from the Set does nothing, because it never was in the Set to begin with. If you comment out line 11 you will see the behavior that you were expecting.


so first i2 ->46
next i changed to i2->47
so when i say set.remove(i2);
is that 47 will get removed??
so there 46 is still there in the set so size is 1 ??
is that what you mean??
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
47 can't be removed from the set, because it is not in the set.

But otherwise, yes - that's what I meant.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to differentiate between objects and object references. After you assign 47 to i2, i2 points to a new object (the value of which is 47), and the set contains an object of value 46. But in your code you no longer have a reference to that object, so you can't easily remove it. Try this code:

Here you have two references to the same object (of value 46). So even after you change one of them to point to a different object (by assigning a new value); the other one still points to the object with value 46.
 
yaswant kumar kumar
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:47 can't be removed from the set, because it is not in the set.

But otherwise, yes - that's what I meant.


ok so i2 now referring to 47 and yes 47 is not on set so when i say set.remove(i2) ;
as you said 47 is not in set and i2 refer a different object 47 so 46 is still in set so size is one is that what you mean??
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, see the code I posted.
 
Jaikiran Pai
Sheriff
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yaswant, please QuoteYourSources for this question.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!