+ 1

Swap 2 variables' value

//Please explain why this statement doesn't work in Java: //a -= b = (a += b) -b; //Thanks public static void main(String[] args) { int a = 5, b = 7; System.out.printf("a = %d, b = %d\n",a,b); //Swap a and b without using another variable //Work in C and Java a += b; b = a - b; a -= b; //But when I use this statement: // a -= b = (a += b) -b; //Only work in C and not in Java System.out.printf("a = %d, b = %d\n",a,b); }

20th Apr 2017, 2:39 AM
Thang Nguyen
Thang Nguyen - avatar
2 Respuestas
20th Apr 2017, 2:43 AM
Manideep
Manideep - avatar
+ 1
@Shikamaru: your statement is easer to understand and works in both C and Java. Nice. But I also want to know the problem in the statement: a -= b = (a += b) -b;
20th Apr 2017, 3:23 AM
Thang Nguyen
Thang Nguyen - avatar