I've seen a lot of techniques for swapping two variables without using a third. Swap two variables without using a temp variable How to swap two numbers without using third variable? [duplicate] Swap 2 values of 2 variables without using a third variable.
Asked
Active
Viewed 1,632 times
-1
-
You use a third variable or you manipulate the specific object. If you want a broad solution then you need to use a third/temp variable in one form or another. For specific data types there are specific solutions, for example you could use math with an integer: x = 1, y = 3, x = x + y, y= x - y, x =x - y – sorifiend Jun 23 '21 at 03:45
-
Does this answer your question? [Swapping two variable value without using third variable](https://stackoverflow.com/questions/1826159/swapping-two-variable-value-without-using-third-variable) – MarsAtomic Jun 23 '21 at 04:26
-
1You are obliged to research your question before you post. You are unlikely to be the only person ever to encounter any particular problem, and there's a good chance that your question has already been asked on this site, not to mention the numerous other resources freely available to you on the Internet. – MarsAtomic Jun 23 '21 at 04:28
-
It seems you already found at least three possible answers. What is your question now? – Thomas Kläger Jun 23 '21 at 05:02
1 Answers
3
I will list three different techniques you can use to swap two numbers without using temp variable in Java:
1. Swapping two numbers without using temp variable in Java
int a = 10;
int b = 20;
System.out.println("value of a and b before swapping, a: " + a +" b: " + b);
//swapping value of two numbers without using temp variable
a = a+ b; //now a is 30 and b is 20
b = a -b; //now a is 30 but b is 10 (original value of a)
a = a -b; //now a is 20 and b is 10, numbers are swapped
System.out.println("value of a and b after swapping, a: " + a +" b: " + b);
2. Swapping two numbers without using temp variable in Java with the bitwise operator
int a = 2; //0010 in binary
int b = 4; //0100 in binary
System.out.println("value of a and b before swapping, a: " + a +" b: " + b);
//swapping value of two numbers without using temp variable and XOR bitwise operator
a = a^b; //now a is 6 and b is 4
b = a^b; //now a is 6 but b is 2 (original value of a)
a = a^b; //now a is 4 and b is 2, numbers are swapped
System.out.println("value of a and b after swapping using XOR bitwise operation, a: " + a +" b: " + b);
3. Swapping two numbers without using temp variable in Java with division and multiplication
int a = 6;
int b = 3;
System.out.println("value of a and b before swapping, a: " + a +" b: " + b);
//swapping value of two numbers without using temp variable using multiplication and division
a = a*b; //now a is 18 and b is 3
b = a/b; //now a is 18 but b is 6 (original value of a)
a = a/b; //now a is 3 and b is 6, numbers are swapped
System.out.println("value of a and b after swapping using multiplication and division, a: " + a +" b: " + b);
You could also do an action like this:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter x:");
int x = scanner.nextInt();
System.out.println("Enter y:");
int y = scanner.nextInt();
System.out.println("Before swap: x=" + x + ", y=" + y);
x = x + y;
y = x - y;
x = x - y;
System.out.println("After swap: x=" + x + ", y=" + y);
}
}