Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - Topic: How to exchange the values of integer variables X and Y in JAVA?
Topic: How to exchange the values of integer variables X and Y in JAVA?
X=X+Y, that is, now X is the sum of X and Y, right?

So now the result of X-Y is the previous x, and then copied to y, written as Y=X-Y,

Now y is the original x, and that x is still the sum of that X+Y, right?

X-Y is now this X, which is quite understandable.

Another method is to declare an empty variable, which is equivalent to changing water for two bottles. You need an empty bottle.

int a = 0;

a = x; Give X to A, and then the bottle of X will be empty.

x = y; Then give y to x, is y also empty? Does x contain y water?

y = a; Then give Y an A. Now there is water in Y? It belongs to X.

They just exchanged. famous