Monday 25 April 2016

Swap two numbers in java

Scenario : You have two variables a and b with values assigned to them as 100 and 101 respectively. Write a program in java using third variable two swap the values within the a and b.
Input :  a = 100 and b = 101 
Output : a = 101 and b = 100

public class swap {

       public static void main(String[] args) {
              int a = 100;
              int b = 101;
              System.out.println("Before swapping, a :"+ a +" and b : "+ b);
              swapValue(a, b);
       }

       public static void swapValue(int a, int b){   //mind the use of static here*
              int temp;                              //temp is the third variable
              temp = a;
              a = b;
              b = temp;
              System.out.println("After swapping, a : "+ a +" and b : "+ b);
       }
}

OUTPUT :
Before swapping, a : 100 and b : 101
After swapping,  a : 101 and b : 100

mind the use of static here*
Try removing static from this line, public static void swapValue(int a, int b) and see what you get.
You will get compile-time error at swapValue(a, b); saying "Cannot make a static reference to the non-static method swapValue(int, int) from the type swap".

Why so? (Learn here)


Don't sit relaxed!
Do you really think the original value of variable a and b has been swapped?

To check it, do just as I instruct. Try to print the value of a and b after the swapValue(a, b) method complete its task. 


public class swap {



       public static void main(String[] args) {

              int a = 100;
              int b = 101;
              System.out.println("Before swapping, a :"+ a +" and b : "+ b);
              swapValue(a, b);
              System.out.println("Is swapping done? a :"+ a +" and b : "+ b);
       }

       public static void swapValue(int a, int b){   //mind the use of static here*
              int temp;                 //temp is the third variable
              temp = a;
              a = b;
              b = temp;
              System.out.println("After swapping, a : "+ a +" and b : "+ b);
       }
}

And surprisingly you will see the println in red would print the original value of a and b. i.e. a = 100 and b = 101

----------------------------------------------------------------------------------------
Even though swapValue() method shows that the value of the two variable is swapped then why this crap???
Reason is pretty simple... Look at these two lines again,
swapValue(a, b);              //a, b are the variables declared in main() method
public static void swapValue(int a, int b)  //here a, b are the local variable of this                                                     // swapValue() method. 

I can also write the second line as,
public static void swapValue(int c, int d)

Now what is happening in above line. The value a, b passed by swapValue(a, b) from the main() method is copied in the local variable c(will hold value of a) and d (will hold value of b) of 
            public static void swapValue(int c, int d){
              int temp;                 //temp is the third variable
              temp = c;
              c = d;
              d = temp;
              System.out.println("After swapping, c : "+ c +" and d : "+ d);
           }
So the logic written inside this method is actually swapping c, d which is local variable of this method containing copied value of a, b passed in calling function swapValue(a, b) from main(). 
Thus we can see it is not the value of a, b passed in the calling function from main() method is swapped but local variable in called function is getting swapped.

Hope I made it clear to you...
If not please ask me again

No comments:

Post a Comment

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
If you are looking for a reference book on java then we recommend you to go for → Java The Complete Reference
Click on the image link below to get it now.