Swapping Two Numbers

Using a temp variable

int a = 5;
int b = 10;

int temp = a;
a = b;
b = temp;

System.out.println("a = " + a); // Output: a = 10
System.out.println("b = " + b); // Output: b = 5

Without Using a temp variable (using addition and subtraction):

int a = 5;
int b = 10;

a = a + b;
b = a - b;
a = a - b;

System.out.println("a = " + a); // Output: a = 10
System.out.println("b = " + b); // Output: b = 5

Without using a temporary variable (using bitwise XOR):

int a = 5;
int b = 10;

a = a ^ b;
b = a ^ b;
a = a ^ b;

System.out.println("a = " + a); // Output: a = 10
System.out.println("b = " + b); // Output: b = 5

Using a single statement

int a = 5;
int b = 10;

b = a + b - (a = b);

System.out.println("a = " + a); // Output: a = 10
System.out.println("b = " + b); // Output: b = 5

using the single statement is by far the best and easiest way to swap two numbers without using a temp variable.