This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes generics question  (sample mocks) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "generics question  (sample mocks)" Watch "generics question  (sample mocks)" New topic
Author

generics question (sample mocks)

Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432


output:2 1 1 [46] ?
also if we commented out line 2 ,then again output is 2 1 1 [46] WHY and HOW?

SCJP 6 | FB : Java Certifications-Help. | India Against Corruption
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
comment line 1 and compile the program. I think you will get your answer.

Also keep in mind wrapper classes are immutable.


SCJP 1.6 96%
Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432

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

Joined: Oct 30, 2009
Posts: 504
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

Joined: Jun 23, 2010
Posts: 432

naaice explanation.
i got it.
thanks for the quick reply.
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
you are most welcome
Ram Narayan.M
Ranch Hand

Joined: Jul 11, 2010
Posts: 247

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


SCJP 6 [SCJP - Old is Gold]
amit mandal
Ranch Hand

Joined: Aug 13, 2010
Posts: 46

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.


ocpjp 6.0 certified.
Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432

yes amit you are right
thanks for reply here,
i got it.
amit mandal
Ranch Hand

Joined: Aug 13, 2010
Posts: 46

You are welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: generics question (sample mocks)
 
Similar Threads
Adding in a Set
Confusion on HashSet
Doubt in function remove in Set