package com.itheima;
public class Test015
{
/**
* 对两个变量的值进行互换
* @param args
*/
public static void main(String[] args)
{
int a = 3, b = 5;
System.out.println("a=" + a + " b=" + b);
//使用第三方变量
int temp = 0;
temp = a;
a = b;
b = temp;
System.out.println("a=" + a + " b=" + b);
//用和的方式
a = a+b;
b = a-b;
a = a-b;
System.out.println("a=" + a + " b=" + b);
//用^(异或)的方式,一个数异或同一个数两次,结果还是这个数,比如7^4^4=7
a = a^b;
b = a^b; //b = (a^b)^b = a
a = a^b; //a = (a^b)^a = b
System.out.println("a=" + a + " b=" + b);
}
}
|
|