Hi Lakshmi,
In C you'll have the prototype as
void swap( int *a, int *b )
In C++,
void swap( int &a, int &b )
There's nothing like this in
Java. Arguments are always passed by value. In order to achieve that effect, you have to put it in an array or bundle it in an object and pass it to the function. Here's some code for you.
Sri!
class Wrapper
{
int aValue;
Wrapper(int value)
{
aValue = value;
}
public void setValue(int value)
{
aValue = value;
}
public int getValue()
{
return aValue;
}
}
public class Swap {
public void doSwap( int [] i, int [] j )
{
int k = i[0];
i[0] = j[0];
j[0] = k;
return;
}
public void doSwap( Wrapper a, Wrapper b )
{
int i = a.getValue();
a.setValue( b.getValue() );
b.setValue( i );
return;
}
public static void main(
String [] args)
{
Swap swap = new Swap();
int [] a1 = { 10 };
int [] b1 = { 20 };
swap.doSwap( a1, b1 );
System.out.println(" a1, b1 = " + a1[0] + ", " +b1[0]);
Wrapper i1 = new Wrapper(100);
Wrapper i2 = new Wrapper(200);
swap.doSwap(i1, i2);
System.out.println("i1, i2 = " + i1.getValue() + " " +
i2.getValue());
}
}