swap fuction in java code example

Example 1: swapping with two variables in java

public class Exercise15 {
  public static void main(String[] args) {
     // int, double, float
   int a, b;
   a = 15;
   b = 27;
   System.out.println("Before swapping : a, b = "+a+", "+ + b);
   a = a + b;
   b = a - b;
   a = a - b;
   System.out.println("After swapping : a, b = "+a+", "+ + b);
 }
 
}

Example 2: 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;
}

Tags:

Misc Example