Java ++ operator

So, I tested the statements because I was helping my friend with java, and I came across a strange programming order. What happens when I run the following code

public static void main(String[] args) { int B = 6; //First console print out System.out.println(B+=++B); System.out.println(B); B = 6; //Second Console print out System.out.println(B+=B++); System.out.println(B); } 

The output for the following code:

 13 13 12 12 

Which causes the second output of the math command B = 12 when it adds 6 to itself, and then ++ (which is +1)

+7
java operators logic
source share
8 answers

The difference here is in increment operators.

In the case of B += ++B , B increases by 7 and is added to its old self (6) to reach 13.

In the case of B += B++ B is added to itself, giving 12, then ++ is executed and the result is stored in B, but the result of the calculation is then saved over ++ in B. Thus, giving 12 as the output.

+8
source share
 ACTION EFFECT NOTES ---------- ------ ----- B = 6 B = 6 B += (++B) B += 7 // B is incremented and the value is returned BB = 13 B = 6 B = 6 B += (B++) B += 6 // The current value of B is the result of (B++) and B is // incremented *after* the (B++) expression; however, the // assignment to B (+=) happens after the (++) but it does // not re-read B and thereby covers up the post increment. BB = 12 
+5
source share

he adds 6 to himself and then ++

This is not exactly what is happening. It adds the result of B++ (i.e. the value that B++ evaluates)) to B B++ evaluates to 6 because the postfix increment operators evaluate the value of the operand before the increment. Therefore, he adds 6 to B

You think the increment should happen after the assignment, but that’s not how ++ works. The increment occurs immediately, but the expression increases the value of B before the increment. Therefore, since you reassign B immediately after B++ , the increment is overridden by the destination.

+2
source share

When it comes to pre-increment and post-increment statements, always remember that they apply immediately before / after the variable in question has been read, and before processing the left side or assignment.

eg.

 int a = 2; a = a++ + a++ + a++; 

a will be equal to 9, since it ends with a = 2 + 3 + 4 ;

Similarly

 int a = 2; a = a++; 

a will still be 2 , since the assignment occurs after the increment, and the increment occurs after reading .

 int a = 2; a = ++a; 

a will be 3, since the assignment occurs after the increment , but the increment occurs before reading .

In your second example, the post-increment occurs after reading, and therefore the increment B never has the ability to be read (not used again in the expression) before the assignment (assignment that overwrites B and any effect on the subsequent increment would be).

If you do:

 int B = 6; int C = B + B++; 

Then you get B equal to 7, and C will be 12. While B++ + B or B + ++B both will have C equal to 13 and B still equal to 7.

+2
source share

b+=x same as b = b + x .

You do b+=b++ , so it can be written as b = b + b++ , which for b=6 will be

 b = 6 + 6; 

because the second parameter will be evaluated first to 6 ( b++ will return the original value first, and then increment b ), and since you assign the result (12 = 6 + 6) to b , the increment does not matter, and b will become 12.

0
source share

rewriting and replacing: B = 6 + 6 after that, B++ is executed because B++ takes the current value from B, then increases, so when B ++ arises, it doesn't matter, the value of B will be 12

0
source share

When you put the increment operator in front of a variable, it increments and displays the variable. When you enter the increment operator after a variable, it shows the variable and then adds to it.

0
source share

++ B means: increment the value and then return it. B ++ means: return the value and then increase it.

0
source share

All Articles