• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic