Example:
Before Swap
number1=45 number2=33
After Swap
number1=33 number2=45
class Swap {
public static void main(String[] args) {
int number1=45,number2=33;
number1 = number1 + number2;
number2 = number1 - number2;
number1 = number1 - number2;
System.out.println("After swapping, number1= " + number1 + " and number2= "
+ number2);
}
}
4 comments:
Here is couple of more way to swap two numbers without using temp variable in Java
Three Methods to do Swapping without using temp variable
One of the best explanation with example. Thanks a lot..
Cheers,
http://www.flowerbrackets.com/java-swap/
Post a Comment