Swap two any numbers (from Keyboard)

This is a simple Java Oriented language program.
If you are newbie in Java programming then our tutorial and example are
helpful for understanding Java programming in the simplest way. Here we will
learn how to swap or exchange the number to each other. First of all we have to create a class
"Swap". Now, we use the Integer.parseInt(args[o]) and Integer.parseInt(args[1])
methods for getting the integer type values in command line. Use a
temporary variable z of type integer that will help us to swap the numbers
Assign the value of x to z variable, now assign the value
of y to the variable x. Lastly assign the value of z to variable y. Now we get the
values has been interchanged. To display the values on the command prompt use
println() method and the swapped values will be displayed.
Here is the code of this program
public class Swap {
public static void main(String[] args) {
int x= Integer.parseInt(args[0]);
int y= Integer.parseInt(args[1]);
System.out.println("Initial value:
"+ x +" and " + y);
int z = x;
x = y;
y = z;
System.out.println("Swapped value:
" + x +" and " + y);
}
}
|
Download this Example.

|