1
a = b-(a-(b = a));  //swapping a and b

also, if a=20,b=10 why does

System.out.println(b = a);

give 20 as result?

  • 1
    why swap variables like that? to confuse people? – Ousmane D. Feb 24 '18 at 20:39
  • 4
    1) Math. 2) Assignment. – Elliott Frisch Feb 24 '18 at 20:39
  • it gives `20` because after `a = b-(a-(b = a));` `a` is `20` and `b` is `10` thus `System.out.println(b = a);` is `20` – Ousmane D. Feb 24 '18 at 20:41
  • Could someone elaborate the first code a bit? It seems that the most outer `b` is 20, why is that? I thought that it goes that way: **1.** assign value of `a` to `b`, so `b` is now 10, **2.** subtract `a - b`, so it's now `10 - 10`, result is 0, **3.** subtract `b - 0`, and I was pretty sure it's now `10 - 0`, but it seems it's `20 - 0` as it's 20 what is assigned to final `a`. I'm really curious where is a hole in my logic. Could someone explain in what part I'm wrong, as it seems it's **3.** in my comment, why is that? – Przemysław Moskal Feb 24 '18 at 20:55
  • 1
    When `a=10,b=20` `a = b-(a-(b = a));` becomes `a = 20-(a-(b = a))` then `a = 20-(10-(b = a))` then `a = 20-(10-(10));` (here we assigned `a` to `b` so `b=10` but we also returned that 10), after that `a=20-(0)` so `a=20`. Swap completed. – Pshemo Feb 24 '18 at 20:59
  • @Pshemo Thank you for giving this explanation. I'm really, really shocked with what happened here as it seems it's a completely basic thing that I missed somewhere. What is the reason of that order of this operation? I was pretty sure that when taking operator precedence into account, operation contained in parenthesis is almost the very first thing to calculate, so I start from the most inner parenthesis and go outer and outer with each calculation/assignment. I'm really curious where lies the mistake that I make... I feel so dumb now :-/ – Przemysław Moskal Feb 24 '18 at 21:11
  • @PrzemysławMoskal I am not sure terms which I will use to explain it will be correct but if I remember it is *associativity* which for `+`,`-` operators is done from left to right. So when we have code like `f()+g()` we are sure that `f()` will be executed first, then `g()`. Similarly when we have `f()+(g()+h())` first `f()` will be evaluated, then `(g()+h())`. Demo: https://ideone.com/HpV1MI. Here is nice question about it: [What are the rules for evaluation order in Java?](https://stackoverflow.com/q/6800590) – Pshemo Feb 24 '18 at 21:24
  • Because of that at `b-(a-(b = a))` first evaluates `b` (which at that point is 20) and then will try to evaluate `(a-(b = a))`. – Pshemo Feb 24 '18 at 21:29
  • [Return value of assignment operation in java](https://stackoverflow.com/q/38163938). `int a=10,b=20; System.out.println(b = a);` most definitely does not print out 20. – Bernhard Barker Feb 24 '18 at 21:47
  • @Pshemo It seems that the term *associativity* is the perfect one in this case. Question that you linked to, as well as the most upvoted answer there made me understand what my problem was, Thank you very much for your time to explain this problem to me with such details, I really appreciate it and hope there will come the day I can return a favor :-) Wish you all the best. PS. I would like to admit that staring at the comment of Elliott Frisch for a certain amount of time shed some light to make me understand the issue. Again, thank you. – Przemysław Moskal Feb 24 '18 at 21:48
  • 1
    @PrzemysławMoskal Actually associativity wasn't the term I was looking for. Associativity helps us when we have one operand which is surrounded by two operators with same precedence (priority) like for `a+b+c` which operand `b` should be used with? Because of `left-to-right associativity of `+` we know that it will be treated as `(a+b)+c`. But `a=b=c` is same as `a=(b=c)` because of right-to-left associativity of `=` operator. Rule which was followed in OP case was "subexpressions are evaluated left to right" which doesn't really have anything to do with associativity or precedence. – Pshemo Feb 24 '18 at 23:17
  • @Pshemo Year, you're right that the correct term is "order of evaluation of subexpressions". It doesn't change that you've shown me where to start looking for an answer as link that you've provided, covered both of topics pretty well. Your explanation lead me then to other sources to find more specific answers to what I was struggling with. You know, when looking at the OP's problem now, I can't believe I messed it up, as it's almost clearly a "Math thing" and not a "Java thing" (except last `(b=a)`, but it could be a single number, 1+1 or anything that simple, as well). Thanks a lot! – Przemysław Moskal Feb 24 '18 at 23:55

1 Answers1

1
a = b-(a-(b = a));  //swapping a and b

It's a way of swapping numeric types without the need of a support variable.

They usually ask you this in interviews.

It is particularly unreadable as it's written in one line, but consider this step by step:

int a = 5;
int b = 2;

a = b - a; // <- -3
b = b - a; // <- 2 - ( - 3) <- 5
a = a + b; // <- (- 3) + 5 = 2

You still can't swap non-numeric types without a temporary variable.


With

System.out.println(b = a);

I guess you wanted to check if b equals a, in which case you should have written System.out.println(b == a), which evaluates to a boolean result.

With b = a, you are assigning a's value to b and then print its result.

payloc91
  • 3,724
  • 1
  • 17
  • 45