C
C
Cyber ​​Cyber2020-07-18 18:31:55
Java
Cyber ​​Cyber, 2020-07-18 18:31:55

How does this expression work in java?

a=a+b-(b=a);
I know that this line swaps the values ​​of the variables a and b, but how did the idea to do this even come about? Why is there a sum at all and why do we subtract the expression in brackets from it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan, 2020-07-18
Hasanly @azerphoenix

As you have already answered, here you need knowledge of the basics of arithmetic and logic. When it comes to programming, pay attention to operator precedence. For example, an expression enclosed in parentheses takes precedence, just like in mathematics.

2 * (2 + 2) = 8
2 * 2 + 2 = 6

The same rules apply to programming.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question