Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • 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...
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • 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: 232
2
  • Mark post as helpful
  • send pies
  • 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: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • 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.
 
Daniel Cox
Ranch Hand
Posts: 147
7
  • Mark post as helpful
  • send pies
  • 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
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • 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: 232
    2
    • Mark post as helpful
    • send pies
    • 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
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic