March 19, 20178 yr Hello everyone, I would like to begin this by mentioning that I am learning Java through the app SoloLearn. I have understood most of what I have covered (which is the entire Basics section) but I did not understand something involving Prefix and Postfix Increment (+) operators. I don't understand why in this example the result is 35 int x=34; int y= ++x but in this it is still 34. int x=34; int y= x++; Can someone help me understand why this is? Thank you!
March 19, 20178 yr 5 minutes ago, PixelToast said: Hello everyone, I would like to begin this by mentioning that I am learning Java through the app SoloLearn. I have understood most of what I have covered (which is the entire Basics section) but I did not understand something involving Prefix and Postfix Increment (+) operators. I don't understand why in this example the result is 35 int x=34; int y= ++x but in this it is still 34. int x=34; int y= x++; Can someone help me understand why this is? Thank you! Because with prefix x is incremented first then the result is assigned to y With postfix the value of x is assigned to y, then x is incremented
March 19, 20178 yr int y= ++x FIRST INCREMENT X THEN ASSIGN THE VALUE OF X TO Y: x = x +1 // First increment x int y = x; // Then assign the value of x to y int y= x++ FIRST ASSIGN THE VALUE OF X TO Y THEN INCREMENT X: int y = x; // First assign the value of x to y x = x+1; // Then increment x