Skip to content

Instantly share code, notes, and snippets.

@asdf913
Created June 5, 2025 08:29
Show Gist options
  • Save asdf913/dce24d35cfb609e1aaac77d0bb1cca0b to your computer and use it in GitHub Desktop.
Save asdf913/dce24d35cfb609e1aaac77d0bb1cca0b to your computer and use it in GitHub Desktop.
Swap Value From Two Variables Without Temporary Variable
public class SwapValueFromTwoVariablesWithoutTemporaryVariable {
public static void main(final String[] args) {
//
int a = 10;
//
int b = 2;
//
System.out.println("a=" + a + ",b=" + b);
//
a = a + b;
//
System.out.println("a=" + a + ",b=" + b);
//
b = a - b;
//
System.out.println("a=" + a + ",b=" + b);
//
a = a - b;
//
System.out.println("a= " + a + ",b=" + b);
//
}
}
@asdf913
Copy link
Author

asdf913 commented Jun 5, 2025

Output

a=10,b=2
a=12,b=2
a=12,b=10
a= 2,b=10

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment