Simply describes three methods of exchanging two variables

We are in the process of learning, experience exchange between two simple variables, usually we will set up a third variable as an intermediary.

Such as:

 

1         int a=10;
2         int b=20;
3         int c;
4         c=a;
5         a=b;
6         b=c;
7         System.out.println("a="+a);
8         System.out.println("b="+b);

A result:

a=20
b=10

Second, we can also do the following:

1         int a=10;
2         int b=20;
3         a=a^b;
4         b=a^b;
5         a=a^b;
6         System.out.println("a="+a);
7         System.out.println("b="+b);

The results II:

a=20
b=10

Third, this may also be:

1      int a=10;
2         int b=20;
3         b=a+(a=b)*0;
4         System.out.println("a="+a);
5         System.out.println("b="+b);

The results of three:

a=20
b=10

 

Guess you like

Origin www.cnblogs.com/newbie273/p/11706525.html