• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

generics question (sample mocks)

 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

output:2 1 1 [46] ?
also if we commented out line 2 ,then again output is 2 1 1 [46] WHY and HOW?
 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
comment line 1 and compile the program. I think you will get your answer.

Also keep in mind wrapper classes are immutable.
 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Neha Daga wrote:comment line 1 and compile the program.

yes, if we commented out line 1
offcourse the output would be : 2 1 0 []
that is my question why in above code output is not 2 1 0 []?
 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok I will explain you line by line:


After line1, set has 2 values 45 & 46 represented by i1 and i2.
Now, after line2, i2 now refers to 47 ans since wrapper objects are immutable it will be a new object and will not change the value of the object added to set.
Now, at line 3 you are trying to remove object refered by i2 i.e 47, which is not there in set so nothing will be removed from set and it will give the output:
2 1 1 [46]

Hope you understand.

 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
naaice explanation.
i got it.
thanks for the quick reply.
 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you are most welcome
 
Ram Narayan.M
Ranch Hand
Posts: 247
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Once the objects are added to the Set... It will be referred by the references in the Set... Here, you added Integer object having "46" to the set... That will be referred by reference in the Set...

In line1, i2 starts to refer to the Integer object "47". It does not mean that Integer object containing 46 will be changed to 47...

In line2, Integer object "47" is given to be removed... Search is made in the Set and removal does not occur since no Integer Object 47 is available...

So, the output is 46 and the size is retained as 1...
 
amit mandal
Ranch Hand
Posts: 46
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
add this line instead of line 12:
System.out.println(set.remove(i2));

you will see that its returning false indicating that it is unable to find i2 with a value of 47. So confirming that 46 still remains in the collection and the rest of the explanation is same as its been told above.
 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes amit you are right
thanks for reply here,
i got it.
 
amit mandal
Ranch Hand
Posts: 46
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic