• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with Iterator.hasNext()

 
Sushma Menon
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any difference between "while (iterator.hasNext()) {" and "while (iterator.hasNext() == true)". Logically both the statements seem correct but while exectuting the latter, sometimes I get unexpected results. While when the former statement is used, always I got the expected result.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No the result is the same. The first solution directly evaluates the result of hasNext(), the second solution first compares the result of hasNext() == true and if this comparison is true then you enter the loop.
Olli
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you please elaborate on the "unexpected results." I can't think of anything that would cause this, as the statements should be equal, except that the latter places an extra operation on the stack. Once that operation is evaluated, everthing else should be equal, side effects included.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sushma_menon,
Welcome to JavaRanch!
We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy. (How's about just replacing that underscore with a space?)
Thanks Pardner! Hope to see you 'round the Ranch!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic