Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials:
 

Software Solutions and Services
 

 
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
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;
    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

                         

» View all related tutorials
Related Tags: c file array class list ui lists method get name using this oo root example where to exam drive store

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

3 comments so far (
post your own) View All Comments Latest 10 Comments:

Hi roseindia,
thanks for the requested solution also please send the code or program to swap two numbers
a)with temporary variables
b)without temporary variables

please kindly send the reply as soo as possible
thanking you
yours faithfully
deepika

Posted by deepika on Monday, 06.2.08 @ 14:23pm | #61804

Actually Java passes method arguments by value.

So even though the value has changed in the method, after the return from that method, in the main function, the values of i and j will be the same.

Posted by Somaiah on Thursday, 03.27.08 @ 19:55pm | #54510

assalam alaikum
oh thanks this site is gr8
becouse it help me in solving my assingment
thanks alot

Posted by Noor-ul-huda on Sunday, 02.18.07 @ 17:39pm | #8303

Training Courses
Tell A Friend
Your Friend Name
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation
 
Search Tutorials:

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.