Generally realize the interchange between two variables to use the third variable, this can be done, but create new variables, increase the system overhead. If you want to exchange variables with two integer variables, you can use a more efficient method. For example: ^ (XOR) operation, examples are as follows:
Import Java.util.Scanner;
public class Variableexchange {public
static void Main (String args[]) {
Scanner scan = new Scanner (system.in);
system.out.println ("Please enter the first integer variable:");
Long A = Scan.nextlong ();
System.out.println ("Please enter a second integer variable:");
Long B = Scan.nextlong ();
System.out.println ("a=" +a+ "\tb=" +b);
System.out.println ("After the execution variable interchange ...");
A = a^b; Execute variable interchange
B = b^a;
A = a^b;
System.out.println ("a=" +a+ "\tb=" +b);
}
Output results:
Run:
Enter the first integer variable: M
Enter the second integer variable:
a=100 b=200
execute variable interchange ...
a=200 b=100 Build
successful (total time:5 seconds)