Home Java Beginners Swapping of two numbers


Share on Google+Share on Google+

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.

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;
  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);

Output of this program is given below:

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

Download this example


If you enjoyed this post then why not add us on Google+? Add us to your Circles

Liked it!  Share this Tutorial

Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: March 11, 2008

Related Tutorials

Ask Questions?    Discuss: Swapping of two numbers   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
April 7, 2011

i need a swap the two number program in pl-sql...
May 25, 2011
This is not what swapping actually is!

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.
June 27, 2011

what is identifier? in java why main() is static? in java why main() takes String as a parameter?
Arun Deo
August 19, 2011
Swaping Integer 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.
October 16, 2011
This is wrong

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.
November 3, 2011

ur tutorials r good..
January 23, 2013

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 :-)
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); }
January 23, 2012

this is swap? this is just WRONG...
April 16, 2012
Very misleading article

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

September 10, 2012

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

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.
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.
November 2, 2012
uesful for all

thank you