Java: Why does this swap method not work? [duplicate]

This doesn’t have anything to do with immutability of integers; it has to do with the fact that Java is Pass-by-Value, Dammit! (Not annoyed, just the title of the article :p )

To sum up: You can’t really make a swap method in Java. You just have to do the swap yourself, wherever you need it; which is just three lines of code anyways, so shouldn’t be that much of a problem 🙂

    Thing tmp = a;
    a = b;
    b = tmp;

Leave a Comment