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?
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?
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.