Every one knows that variable swapping in C/C++ is very simple, just exchanging values

swapping variable in Java and C++

void  swap ( int & a, int & b)    
{    
    int  Temp;    
    temp = a;    
    a = b;    
    b = temp;    
}

But in Java this approach will not work or I would say is not feasible, because java does not support ‘pass by reference’, Java is always pass-by-value. So how you program it.

Method 1: Using Array to swap variable in java

Public  static  void  swap ( int [] Data,  int  a,  int  b) {    
        int  t = Data [a];    
        data [a] = data [b];    
        data [b] = t;    
}

Method 2: Using redefined classes

/ / MyInteger: with Integer somewhat similar, but the value of the object can be changed    
Class  MyInteger {       
    Private  int  x;     / / the x as the only data member     
    Public  MyInteger ( int  xIn) {x = xIn;}  / / Constructor     
    Public  int  getValue () {  return  x;}   / / get the value      
    Public  void  insertValue ( int  xIn) {x = xIn;}  / / change the value of the    
}    
 
Public  Class  Swapping {       
    / / Swap: pass object references     
    static  void  swap (MyInteger rWrap, MyInteger sWrap) {          
        / / Change the value of the process         
        int  t = rWrap.getValue ();          
        rWrap.insertValue (sWrap.getValue ());          
        sWrap.insertValue (t);       
    }       
    Public  static  void  main (String [] args) {          
        int  a =  23 , b =  47 ;          
        System.out.println ( "Before. a:"  + a +  ", b:"  + b);          
        MyInteger AWRAP =  new  MyInteger (a);          
        MyInteger bWrap =  new  MyInteger (b);          
        swap (aWrap, bWrap);          
        a = aWrap.getValue ();          
        b = bWrap.getValue ();          
        System.out.println ( "After. a:"  + a +  ", b:"  + b);       
    }    
}

Method 3: Using external inline way

Public  Class  Swap2 {     
    Public  static  void  main (String args []) {     
        Swap2 SW =  new  Swap2 ( 1 , 2 );     
        System.out.println ( "i is"  + sw.i);     
        System.out.println ( "J is"  + sw.j);     
        sw.swap ();     
        System.out.println ( "i is"  + sw.i);     
        System.out.println ( "J is"  + sw.j);     
    }     
    int  i, J;     
    Public  Swap2 ( int  i,  int  J) {     
        this . i = i;     
        this . J = J;     
    }     
 
    Public   void  swap () {     
        int  Temp;     
        temp = i;     
        i = j;     
        j = temp;     
    }     
}     
 
Public  Class  swap1 {     
    Public  static  void  swap1 (Integer a, Integer b) {     
        Integer temp = a;     
        a = b;     
        b = temp;     
    }     
    Public   static   void  main (String args []) {     
        Integer a, b;     
        a =  new  Integer ( 10 );     
        b =  new  Integer ( 20 );     
        Swap1.Swap1 (a, b);     
        System.out.println ( "a is"  + a);     
        System.out.println ( "b is"  + b);     
    }     
}

You can read more about java passed by value reference and how we can achieve swapping in C++ and Java