• Post Reply Bookmark Topic Watch Topic
  • New Topic

I'm confused with increment  RSS feed

 
Alex Kam
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

can you explain me why it throws an compilation error (an endless cycle), please:

for(int i = 0; i < 10; ){
i = i++;
System.out.println(i);
}

As for my logic:

1. Initialize i with 0;
2. As the post-increment is present, assign 0 to i (because i is equal to 0);
3. Now i is equal to 0, but we have to increment it. Thus i is 1 after '++' (because i++ is the same as i = i + 1);

But it doesn't work that way. Why not? Why i isn't incremented to 1 at first iteration?
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

produces this bytecode (the variable i is stored at index 1):

Which is:
ILOAD - load the variable.
IINC - increment the local variable (not the value loaded, the actual variable) by 1
ISTORE - store the value loaded (the value from ILOAD) back into the variable.

So in order the values are, during the three steps:
1. (ILOAD) variable = 0, loaded value = 0
2. (IINC) variable = 1, loaded value = 0
2. (ISTORE) variable = 0, loaded value = 0

In summary, it sticks the original value (0) to one side. Increments the variable (to 1), then reassigns the stored value (0) back to the variable. Consequently in your code the value of i never changes, so it's an infinite loop.

If it were a pre-increment (i = ++i) then the bytecode would be:

Which would result in the variable being incremented, and then loaded...so the incremented value isn't lost.
 
Alex Kam
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow, it seems to be so confusing..

Dave Tolls, thank you for your explanation! Now it is much clearer.
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, there is no point of writing i = ++i.
Just write i++.
 
Alex Kam
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:Also, there is no point of writing i = ++i.
Just write i++.


Yeah, sure, I'm just preparing for OCAJP 8 and found this example at a book. So I was sure there weren't mistakes at all and i would be incremented.

Thank you!
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alex Kam wrote:Wow, it seems to be so confusing..

Dave Tolls, thank you for your explanation! Now it is much clearer.


Glad to be of help.
I was a bit concerned I'd gone into too much detail.

As Pawel says, though, you wouldn't do i= ++i...I only stuck that in to show the difference in byte code.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question has come up so often we actually have a FAQ for it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!