Is it possible to write swap method in Java?
Without using an array or objects, no, it is not possible to do it within a method.
Java method to swap primitives
You can't create a method swap, so that after calling swap(x,y)
the values of x and y will be swapped. You could create such a method for mutable classes by swapping their contents¹, but this would not change their object identity and you could not define a general method for this.
You can however write a method that swaps two items in an array or list if that's what you want.
¹ For example you could create a swap method that takes two lists and after executing the method, list x will have the previous contents of list y and list y will have the previous contents of list x.
How to code a swap() method, or alternatives?
As you understand, the only way to swap to primitives by a method works only if they are in global scope or class scope.
As for arrays and objects, they are passed by reference in Java. So any changes that you make changes the actual argument to the method. On the other hand if you are looking for a neat way to swap two objects, just implement a swap method as a method in the class and call it like:
anObject.swap(anotherObject);
This way you can implement the swap however you want.
Swap two int values
Your problem is that Java (when it comes to primitive values such as ints) is "pass-by-value", while your code inherently assumes "pass-by-reference".
See https://www.javaworld.com/article/2077424/learn-java-does-java-pass-by-reference-or-pass-by-value.html as a great description of why this is. In short, when the function is called, the parameters are essentially "copied" into the function as new variables that are distinct from the original, although they have the same value. Thus, those variable's contents change independently of the parent function variables' contents.
I want to swap two numbers by using another class with a swap method and not the general swap function which is commonly available
You are essentially swapping the two numbers twice, and therefore not swapping them at all:
swapper s = new swapper(a[j],a[j+1]); // this assigns a[j] to s.x and a[j+1] to s.y
s.swap(); // this swaps s.x and s.y
a[j] = s.y; // this assigns the original value of s.x (a[j]) to a[j]
a[j+1] = s.x; // this assigns the original value of s.y (a[j+1]) to a[j+1]
In order for the swapping to work as expected, change it to:
swapper s = new swapper(a[j],a[j+1]);
s.swap();
a[j] = s.x;
a[j+1] = s.y;
Java: Why does this swap method not work?
This doesn't have anything to do with immutability of integers; it has to do with the fact that Java is Pass-by-Value, Dammit! (Not annoyed, just the title of the article :p )
To sum up: You can't really make a swap method in Java. You just have to do the swap yourself, wherever you need it; which is just three lines of code anyways, so shouldn't be that much of a problem :)
Thing tmp = a;
a = b;
b = tmp;
Related Topics
Variable Is Accessed Within Inner Class. Needs to Be Declared Final
Why I Cant Use the Populateviewholder Override Method
Countdowntimer in Minutes and Seconds
Garbage Collector Log (Loggc) File Rotation with Logrotate Does Not Work Properly
What Is the Point of Heterogenous Arrays
Nullpointerexception in Java with No Stacktrace
Get Domain Name from Given Url
How to Delete Stuff Printed to Console by System.Out.Println()
Wanting a Type of Grid for a Pixel Editor
Difference Between 'Optional.Orelse()' and 'Optional.Orelseget()'
What's the Syntax to Import a Class in a Default Package in Java
Run Jar File in Command Prompt
Strip Leading and Trailing Spaces from Java String
Java Map with Values Limited by Key's Type Parameter
How to Derive Module Descriptor: Provider {Class X} Not in Module