Your code does not swap the values. Swapping is used mostly in data structures and algorithms. Your swap method is just assigning the values to variables and you are printing the values in the swap method. Print the after calling swap and you'll know it doesn't actually swap values. Java is pass-by-value language. You can only do swapping in Objects especially when they are part of arrays. E.g.
public static void swap(Object[] array, int x, int y) {
Object temp = array[x];
array[x] = array[y];
array[y] = temp;
}
Now this actually swap values.
You called swap(i,j); method in main the result is getting printed inside swap itself. what if values of i and j are printed in main method just next in line after swap(i,j). It will i= j=2. So its not swaping.
This is wrong as it is not swapping i and j actually. You are merely swapping there copies thus even after the function is called i and j will retain their values.
This is simply wrong and won't work this way. Try moving the System.out.println("After swapping..."); after the swap() call and see for yourself. i and j are not swapped, big surprise :-)
You can not swap it like this in Java through a method, java is pass by value strictly unlike C, and it does not have pointers. .
You have just printed the output in the swap method, however, u should have swapped the original values given to you in main method.
if you remove your println statement from swap method and put these change ur main method like this, you will come to know that your values were not changed at all.
.... main(....){
int a=1;
int b=2;
System.out.println("Before swapping a = " + a + " b = " + b);
swap(a,b);
System.out.println("After swapping a = " + a + " b = " + b);
}
This swap function is very misleading. Java passes the values on the stack, so as soon as the 'swap' function returns, i and j lose their values.
Move the:
System.out.println("After swapping i = " + i + " j = " + j);
to after the swap function is called and you'll see.
It seems to swap, but it doesn't.
Actually the values passed as parameters are assigned to a "new variables", the parameters themselves.
So the original variables still having the same values.
The numbers were NOT swapped after the swap() method was called. The COPIES of the numbers were indeed swapped.
So your code is incorrect at best. I might even add it's dishonest.
PL/SQL PROGRAM.....vikash April 7, 2011 at 10:30 PM
i need a swap the two number program in pl-sql...
This is not what swapping actually is!Farrukh May 25, 2011 at 12:13 AM
Your code does not swap the values. Swapping is used mostly in data structures and algorithms. Your swap method is just assigning the values to variables and you are printing the values in the swap method. Print the after calling swap and you'll know it doesn't actually swap values. Java is pass-by-value language. You can only do swapping in Objects especially when they are part of arrays. E.g. public static void swap(Object[] array, int x, int y) { Object temp = array[x]; array[x] = array[y]; array[y] = temp; } Now this actually swap values.
javasiva June 27, 2011 at 10:24 PM
what is identifier? in java why main() is static? in java why main() takes String as a parameter?
Swaping Integer valuesArun Deo August 19, 2011 at 10:17 PM
You called swap(i,j); method in main the result is getting printed inside swap itself. what if values of i and j are printed in main method just next in line after swap(i,j). It will i= j=2. So its not swaping.
This is wrongshivendra October 16, 2011 at 4:50 PM
This is wrong as it is not swapping i and j actually. You are merely swapping there copies thus even after the function is called i and j will retain their values.
comentkanika November 3, 2011 at 11:53 AM
ur tutorials r good..
WrongXiaoP January 23, 2013 at 8:07 PM
This is simply wrong and won't work this way. Try moving the System.out.println("After swapping..."); after the swap() call and see for yourself. i and j are not swapped, big surprise :-)
This is stupid.. u cant swap like this..Waqas December 28, 2011 at 1:25 AM
You can not swap it like this in Java through a method, java is pass by value strictly unlike C, and it does not have pointers. . You have just printed the output in the swap method, however, u should have swapped the original values given to you in main method. if you remove your println statement from swap method and put these change ur main method like this, you will come to know that your values were not changed at all. .... main(....){ int a=1; int b=2; System.out.println("Before swapping a = " + a + " b = " + b); swap(a,b); System.out.println("After swapping a = " + a + " b = " + b); }
wrong!abc January 23, 2012 at 6:00 AM
this is swap? this is just WRONG...
Very misleading articleLeslie April 16, 2012 at 9:33 AM
This swap function is very misleading. Java passes the values on the stack, so as soon as the 'swap' function returns, i and j lose their values. Move the: System.out.println("After swapping i = " + i + " j = " + j); to after the swap function is called and you'll see.
javagurujavaguru May 2, 2012 at 2:55 PM
Amanzing.
typeMIrroImage September 10, 2012 at 3:28 AM
1. "t" missing in "println" on line 12. Should be: System.out.println(...) 2. This swap function does NOT work in Java
wrongbee December 17, 2012 at 11:05 PM
your code can't swap 2 numbers! i,j only swap in swap(i,j) and not swap in main LOL!
It doesn't swap at alljaime martinez October 16, 2012 at 12:08 AM
It seems to swap, but it doesn't. Actually the values passed as parameters are assigned to a "new variables", the parameters themselves. So the original variables still having the same values.
Swapping Valuesmouka October 30, 2012 at 10:32 PM
The numbers were NOT swapped after the swap() method was called. The COPIES of the numbers were indeed swapped. So your code is incorrect at best. I might even add it's dishonest.
uesful for allraj November 2, 2012 at 3:08 PM
thank you
Post your Comment