Friday, February 18, 2011

Swapping of two numbers works in case of static function

Consider the case of swapping 2 integers. We swapped them earlier by boxing them to object. But it works in case of static function as well:

 

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 is they are swapped. Smile with tongue out

No comments:

Post a Comment

Chitika