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 Mixing Generic and Non-generic Collections pg no 601 K and B 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 "Mixing Generic and Non-generic Collections pg no 601 K and B" Watch "Mixing Generic and Non-generic Collections pg no 601 K and B" New topic
Author

Mixing Generic and Non-generic Collections pg no 601 K and B

kiruthigha rajan
Ranch Hand

Joined: Dec 29, 2011
Posts: 69

Hai im having doubt in this line

int i = ((Integer)it.next()).intValue();

1)why casting is done here and why we have used intValue().

2) Im having doubt in hasNext() execution..here there are two elements.
in the first hasNext() test the value is true and it enters into the loop and it points to the next element.
when again the hasNext() is checked it should return false as there no element after 6.then how it is executed??

please explain in detail the execution of hasNext() in detail thanks in advance
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
kiruthigha rajan wrote:1)why casting is done here and why we have used intValue().

Since the List is not generic and contains only of type Object, it is type cast to Integer. The intValue() is a method of the Integer class to get its contained int value.
kiruthigha rajan wrote:in the first hasNext() test the value is true and it enters into the loop and it points to the next element.
when again the hasNext() is checked it should return false as there no element after 6.then how it is executed??

In the first call to hasNext() it checks the List and returns true since 4 will be returned while a call to next() method.
In the second call to hasNext() it again returns true since 6 will be returned on calling next() method.
Only in the third hasNext() call, the while loop condition fails.

Read - intValue method and hasNext method
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Mixing Generic and Non-generic Collections pg no 601 K and B
 
Similar Threads
Generics warnings... I don't understand?!
Problem on Generic example from K&B study guide
Generic--- ClassCastException
Generics query
Need Help on Generecs and non-Generics