Home Java Beginners Swapping of two numbers
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Swapping of two numbers

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

Download this example

Advertisements

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
 
 
Comments
vikash
April 7, 2011
PL/SQL PROGRAM.....

i need a swap the two number program in pl-sql...
Farrukh
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.
siva
June 27, 2011
java

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

ur tutorials r good..
XiaoP
January 23, 2013
Wrong

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 :-)
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
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.
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
DMCA.com