java swap function code example
Example 1: 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 2: 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
Example 3: swapping values in java using a function not working
#include <stdio.h>
int main() {
int a = 23, b = 47;
int t;
printf("Before. a: %d, b: %d\n", a, b);
t = a;
a = b;
b = t;
printf("After. a: %d, b: %d\n", a, b);
return 0;
}