How to write a basic swap function in Java

Melinda picture Melinda · Sep 2, 2010 · Viewed 183.6k times · Source

I am new to java. How to write the java equivalent of the following C code.

void Swap(int *p, int *q)
{
   int temp;
   temp = *p;
   *p = *q;
   *q = temp;
} 

Answer

Eng.Fouad picture Eng.Fouad · Oct 29, 2013

Here is one trick:

public static int getItself(int itself, int dummy)
{
    return itself;
}

public static void main(String[] args)
{
    int a = 10;
    int b = 20;

    a = getItself(b, b = a);
}