[ACCEPTED]-Is it possible to write swap method in Java?-primitive-types
While it is not possible to write a function 12 that simply swaps two variables, it is possible 11 to write a helper function that allows you to:
- Swap two variables using only one statement
- Without temporary variables in the caller's code
- Without 'boxing' primitives
- With a few overloads (one of them using generics), it works for any type
That's how 10 you could do it:
int returnFirst(int x, int y) {
return x;
}
int a = 8, b = 3;
a = returnFirst(b, b = a); // try reading this as a = b; b = a;
System.out.println("a: " + a + ", b: " + b); // prints a: 3, b: 8
This works because the Java 9 language guarantees (Java Language Specification, Java 8 SE 7 Edition, section 15.12.4.2) that all 7 arguments are evaluated from left to right 6 (unlike some other languages, where the 5 order of evaluation is undefined), so the 4 execution order is:
- The original value of
b
is evaluated in order to be passed as the first argument to the function - The expression
b = a
is evaluated, and the result (the new value ofb
) is passed as the second argument to the function - The function executes, returning the original value of
b
and ignoring its new value - You assign the result to
a
If returnFirst
is too long, you 3 can choose a shorter name to make code more 2 compact (e.g. a = sw(b, b = a)
). Use this to impress your 1 friends and confuse your enemies :-)
Without using an array or objects, no, it 1 is not possible to do it within a method.
Check out this JavaWorld article that explains 7 it in detail:
http://www.javaworld.com/javaworld/javaqa/2000-05/03-qa-0526-pass.html
A swap of two primitives will 6 never work because primitives are passed 5 by value in Java. You can't even write a 4 method to swap two objects for that matter.
Like 3 @Thomas said, the only thing you could do 2 is have your primitives contained within 1 other objects/arrays and modify those.
One-liner for any primitive numbers:
a += (b - (b = a));
0
You can make a generic version of @marcus's 2 swap method that swaps any number of objects 1 of the same type:
<T> T swap(T... args) { // usage: z = swap(a, a=b, b=c, ... y=z);
return args[0];
}
b = swap(a, a=b);
z = swap(x, x=y, y=z);
In java5, the closest I can think of, which 2 may help you, is :
The AtomicInteger class 1 (and others) have getAndSet()
atomic methods ..
To write a swap method that swaps primitives 4 you'd have to have the concept of "out" variables, i.e. variables 3 whose values are passed up to the calling 2 context. C# has those but you must still 1 specify that they're out variables.
This function will swap two ints
Integer[] swap(int a, int b){
return new Integer[]{b,a};
}
0
Here's a method that swaps two primitive 4 variables
private void swap(){
int a = 1;
int b = 2;
int temp = a;
a = b;
b = temp;
}
It might not be of much use though 3 ;)
Ok seriously, it could be done if the 2 variables are class level:
public class MyClass{
// excuse horrible coding practice of public mutable fields
public int a = 1;
public int b = 2;
public void swap(){
int temp = a;
a = b;
b = temp;
}
}
Again though, I 1 fail to see what the use of this could be
I have read the above answers seeking an 4 explanation as to why it is said that a 3 swapping program cannot be written in java 2 in the way it is written in c++. I did 1 the following way program screenshot
As Thomas Owens said. You could probably 2 do it in C by passing variables by &reference, but 1 afaik not in Java without using objects.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.