Pre- and postincrement in Java

后端 未结 6 1702
不思量自难忘°
不思量自难忘° 2021-01-17 13:48

I just wanted to create a little Java-Puzzle, but I puzzled myself. One part of the puzzle is:

What does the following piece of code do:



        
6条回答
  •  悲哀的现实
    2021-01-17 14:40

    it's very easy to understand how it works if you imagine it how java stores values in registers! he puts 1 in the first register, and than goes through = sign, and increments the i(++i), so now in i you have 2, and in the second register you have 2, but the first register is not updated, in the third register you'll have 2 and then i is incremented, and then i is incremented and in the last register you'll have 4. So you'll have something like this 1 = 2 + 2 + 4 == 9

提交回复
热议问题