# Swapping of two numbers

This Java programming tutorial will teach you the methods for writing program to calculate swap of two numbers. Swapping is used where you want to interchange the values. This program will help you to increase your programming ability.

Tutorials

# Swapping of two numbers

This Java programming tutorial will teach you the methods for writing program to calculate swap of two numbers. Swapping is used where  you want to interchange the values. This program will help you to increase your programming ability.

In this program we will see how we can swap two numbers. We can do this by using a temporary variable which is used to store variable so that we can swap the numbers. To swap two numbers first we have to declare a class Swapping. Inside a class declare one static method swap(int i, int j) having two arguments, the value of these arguments will be swapped. Now declare one local variable temp which will help us to swap the values. At last call the main method inside of which you will call the swap method and the result will be displayed to you.

Code of the program is given below

 ``` public class Swapping{   static void swap(int i,int j){   int temp=i;   i=j;   j=temp;   System.out.println("After swapping i = " + i + " j = " + j);   }   public static void main(String[] args){   int i=1;   int j=2;     System.out.prinln("Before swapping i="+i+" j="+j);   swap(i,j);     } }```

`Output of this program is given below:`

 C:\java>java Swapping Before swapping i = 1  j = 2  After swapping i = 2  j = 1

# Swapping of two numbers

Related Tutorials

Discuss: Swapping of two numbers   View All Comments

Subject (*):

Waqas
December 28, 2011
This is stupid.. u cant swap like this..

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); }
abc
January 23, 2012
wrong!

this is swap? this is just WRONG...
Leslie
April 16, 2012

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.
javaguru
May 2, 2012
javaguru

Amanzing.
MIrroImage
September 10, 2012
type

1. "t" missing in "println" on line 12. Should be: System.out.println(...) 2. This swap function does NOT work in Java
bee
December 17, 2012
wrong

your code can't swap 2 numbers! i,j only swap in swap(i,j) and not swap in main LOL!
jaime martinez
October 16, 2012
It doesn't swap at all

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.
mouka
October 30, 2012
Swapping 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.
raj
November 2, 2012
uesful for all

thank you