-1

It is one of the simple operations and I do not understand why and I just save it, but this bothers me, so I want to understand why?

int a = 2;
int b = 4;
int c = 10;
a = b++;
System.out.println(a);
a = ++b;   
System.out.println(a);    

This process ++ I did not understand how it works. In this case, the output is 4 and 6

But I only added 1 and the result was an added 2

In the beginning, she only took the value 4 without added any thing, then in the second output, she added 2

If you put the code in the sixth line a = ++c, the output will be by adding 1, not 2, to c.

so Some help to understand please.

On the other hand, since = is an assignment, I imagine that the result will preserve the last number, and now if I modify the code as follows:

int a = 2;
int b = 4;
int c = 10;
a = b++;
System.out.println(a);
a = ++c;   
System.out.println(a);
a = ++b;
System.out.println(a);

Here the output will be

4
11
6

I did not expect this either, as the last output should have been 5, not 6.

So I would like some help, please, so that I can understand and not just memorize.

5
  • 1
    In the case a = b++;, it adds 1 to b, but it copies the OLD value into a. Thus, a will be 4, but b will be 5. When you do a = ++b;, it bumps b by 1 BEFORE it does the copy. b becomes 6, so that's what is stored in a. The original value of a is never used. Your second example bumps b TWICE. It becomes 6. Commented Dec 3, 2023 at 7:12
  • okay but a = b++; why a become b by 1, What is the benefit of this operation that you performed? Commented Dec 3, 2023 at 7:22
  • I think I understood somewhat, and this means that a = b++ are two operations, not just one operation. One of the operations is assigning b to a, and the other is adding 1? Commented Dec 3, 2023 at 7:28
  • a = b++; -> a = b; b = b + 1;, whereas a = ++b; -> b = b + 1; a = b;. Commented Dec 3, 2023 at 7:32
  • yes, a = b++ are two operations: 1) b++, a post-increment operation that results in the value before the increment was done; 2) a = <result from b++>, an assignment that stores the result of previous point in a || difference between b++ and ++b: the first one results in the value before incrementing; the second results in the value after incrementing (b will always be incremented) || Also note that a = b; b = b + 1; is not always the correct replacement: the assignment is done after the incrementing Commented Dec 3, 2023 at 7:58

2 Answers 2

3

pre increment: a = ++b is equivalent to b = b + 1; a = b;

post increment: a = b++ is equivalent to a = b; b = b + 1;

Sign up to request clarification or add additional context in comments.

Comments

0

The rule to remember is: Think from left to right.

int a = 2;
int b = 4;
a = b++; // First assign b to a, then increment b.
// Here a=4, b=5
a = ++b; // Cannot set a to ++. First increment b, then assing the incremented value to a.
// Here a=6, b=6

3 Comments

Glad to see it heps. In confusing or hard to remember situations like that, I tend to put a rule to remember. This always works for me.
@user85421: Yup, you are right. Precedence is first of course! My rule here is valid only for this (increment operator) context. Maybe I should edit my answer to prevent misunderstanding.
My rule here is not a real rule of course and the operator precedence is well defined. My intention is to put a heuristic to help remember something confusing.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.