This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization 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:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

generics question (sample mocks)  RSS feed

 
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?
 
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
 
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...
 
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
 
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!