• 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

Infinite loop, Question 9 on page 96 (Java OCA 8 Programmer I Study Guide, Sybex)

 
Ranch Hand
Posts: 234
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys, on page 96 of the OCA book, question 9, not sure I understand that.

This they say, generates an infinite loop. Not sure why though, as the variable i is incremented with i=i++. I mean i++ means we add 1 to i each time it executes...
 
Bartender
Posts: 1251
87
Hibernate jQuery Spring MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes i++ does increment  i by 1 but the previous value of i i.e. 0 which was pushed on operand stack is assigned to i so again i becomes 0. This is why i always remains 0 and loop repeats infinitely.
You can refer this post
 
Jason Attin
Ranch Hand
Posts: 234
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks. Not sure I understand, sorry. I had a look at the other post and I think it's still not clear to me. I tried with a similar example, but I still don't get it.

i is still 9 after the assignment. So i starts off with a value of 9. Then we have which we can write as I would have thought...
 
Ganesh Patekar
Bartender
Posts: 1251
87
Hibernate jQuery Spring MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JVM uses operand stack for asigning values to local variables and these operations. It keeps array of these local variables.
Here int i = 9 means first int 9 value is pushed on operand stack then pops 9 value from operand stack and assign It to i.

i = i++; doesn't mean 9 = 9+1, in general we can see It like i  = 9 + 1; because at left side of = we need something to store the result so here i is a varaible Or we can say a storage to store the result so It can't be a digit.

Here i = i++; might look i = 9 + 1 but JVM first pushes the value of i i.e. 9 on operand stack then ++ is executed which directly increments value of local variable i by 1 so i becomes 10 now, but after that JVM pops value which was on operand stack i.e. 9 and assigns to i so again i becomes 9.
 
Ranch Hand
Posts: 234
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In this code


there are 2 operations in the expression i = i++ and the ++ operator will operate before the = operator since it has a higher precedence.

Operation 1 (++ operator)
i++ will do 2 things:
  • i++ evaluates to 9 (at this point i = i++ becomes i = 9 but the i = 9 assignment operation is not executed yet)
  • i++ increments i by 1 (at this point, a separate i = 10 assignment operation is executed and a value of 10 is assigned to i)

  • Operation 2 (= operator)
    The assignment operation i = 9 is executed and a value of 9 is assigned to i
     
    Sheriff
    Posts: 11604
    178
    Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Jason Attin wrote:Not sure why though, as the variable i is incremented with i=i++. I mean i++ means we add 1 to i each time it executes...


    In this topic you'll find an explanation for exactly the same question. And in this topic you'll find an extensive discussion with lots of code examples. Both are definitely worth reading!

    Hope it helps!
    Kind regards,
    Roel
     
    Jason Attin
    Ranch Hand
    Posts: 234
    2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    thanks for the explanation and links, now it's clear. And the funny thing is that now that it's clear, it's so obvious!
    thanks
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic