Zeus
Zeus

Reputation: 2253

Swapping numbers in place

I'm new to Java, thus the question. I'm trying to swap two numbers without using a third temporary variable. The code is fairly simple.

public static void swapInPlace(int a, int b){
        a = a + b;
        b = a - b;
        a = a - b;
    }

    public static void main(String[] args) {
    // write your code here
        int a = 12;
        int b = 7;
        swapInPlace(a, b);
        System.out.println("a: " + a + " b: " + b);

    }

The logic runs fine in the debugger, but outputs the same values for a and b. I understand the same is happening because the copied values were used by the swapInPlace function.

Now if I have to make this function work in C++, I would just change my method signature as

public static void swapInPlace(int& a, int& b)

But since that can't be done in java, my question is how can I make this thing work in java.

Upvotes: 1

Views: 654

Answers (5)

user15572701
user15572701

Reputation:

I think you forgot to send the values to the function by "reference". You are passing values rather than their reference address hence values don't change even after the function is called.

This code will sort your issue

public static void swapInPlace(int &a, int &b){
        a = a + b;
        b = a - b;
        a = a - b;
    }

    public static void main(String[] args) {
    // write your code here
        int a = 12;
        int b = 7;
        swapInPlace(a, b);
        System.out.println("a: " + a + " b: " + b);

    }

Upvotes: 0

A ( probably controversial ) way to achieve this is simply to use a macro pre-processor.

Java may not by default include support for macros, but there is nothing preventing you using a preprocessor. I do use the C preprocessor myself with Java from time to time.

It's worth noting that Java people tend to foam at the mouth when anyone suggests this. There's no rational reason for this but chronic fear, IMO, as preprocessors are perfectly legitimate programming tools. The Java compiler is not friendly to preprocessors, but there are ways around this ( I wrote a compiler modification for this purpose but it's not required to use a preprocessor, it's just convenient for me ).

There are many preprocessors out there. Notable choices are the C preprocessor and the generic preprocessor ( GPP ). The venerable M4 system also leaps to mind.

Upvotes: 1

Frakcool
Frakcool

Reputation: 11163

You have 2 options, create an inner class and create an object of it, this class could have both variables and return the object in your swapInPlace() method.

Another one could be to pass an array, for example:

class Swap {
    public static void swapInPlace(int array[]){
        array[0] = array[0] + array[1];
        array[1] = array[0] - array[1];
        array[0] = array[0] - array[1];
    }

    public static void main(String[] args) {
    // write your code here
        int array[] = new int []{12, 7};
        swapInPlace(array);
        System.out.println("a: " + array[0] + " b: " + array[1]);

    }
}

And the output of the above program is:

a: 7 b: 12

Upvotes: 1

BlueMoon93
BlueMoon93

Reputation: 2970

As other answers have said, you need to use a wrapper. This is a short example on how to do it.

private class IntWrap{
    int value;

    public IntWrap(int v){ this.value=v; }
}

public static void swapInPlace(IntWrap a, IntWrap b){
    a.value = a.value + b.value;
    b.value = a.value - b.value;
    a.value = a.value - b.value;
}

public static void main(String[] args) {
    IntWrap a = new IntWrap(12);
    IntWrap b = new IntWrap(7);
    swapInPlace(a, b);
    System.out.println("a: " + a.value + " b: " + b.value);

}

Upvotes: 1

Denis Kulagin
Denis Kulagin

Reputation: 8947

Java is always pass by value. In some cases, though, you are passing a reference and thus is able to change contents of an object.

You can use AtomicInteger as a mutable wrapper of int for example purpose.

import java.util.concurrent.atomic.AtomicInteger;

public class SwapInPlaceMain {
    public static void swapInPlace(AtomicInteger a, AtomicInteger b) {
        a.set(a.get() + b.get());
        b.set(a.get() - b.get());
        a.set(a.get() - b.get());
    }

    public static void main(String[] args) {
        // write your code here
        AtomicInteger a = new AtomicInteger(12);
        AtomicInteger b = new AtomicInteger(7);
        swapInPlace(a, b);
        System.out.println("a: " + a + " b: " + b);
    }
}

Upvotes: 3

Related Questions