Create a class, define two variables in the class and specify the initial value for it, and then exchange the values of the two variables. It requires that the third variable is not allowed, and only the exchange of the values of the two variables can be achieved using XOR run.
The code copy is as follows:
import java.util.Scanner;
public class VariableExchange {
public static void main(String[] args){
System.out.println("Please enter the value of variable A");
Scanner scanner=new Scanner(System.in);
long A=scanner.nextLong();
System.out.println("Please enter the value of variable B");
Scanner scannerB=new Scanner(System.in);
long B=scannerB.nextLong();
System.out.println("A="+A+"/t"+"B="+B);
System.out.println("Execute variable swap...");
A=A^B;
B=B^A;
A=A^B;
System.out.println("A="+A+"/t"+"B="+B);
}
}
The effect is shown in the figure: