swap JAVA code example
Example 1: java array swap
public static void swap(int x, int y, int[] arr) {
int temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
}
Example 2: swap function java
public static void swap(int[] a, int i, int j){
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
Example 3: java swap variables
// a, b can be a numeric value
a += b; // a = a + b
b = a - b; // b = (a + b) - b = a ---> b=a
a -= b; // a = (a + b) - (a) = b ---> a=b