Java: Why does this swap method not work? -
i have following code:
public class main { static void swap (integer x, integer y) { integer t = x; x = y; y = t; } public static void main(string[] args) { integer = 1; integer b = 2; swap(a, b); system.out.println("a=" + + " b=" + b); } }
i expect print a=2 b=1, prints opposite. swap method doesn't swap , b values. why?
this doesn't have immutability of integers; has fact java pass-by-value, dammit! (not annoyed, title of article :p )
to sum up: can't make swap method in java. have swap yourself, wherever need it; 3 lines of code anyways, shouldn't of problem :)
thing tmp = a; = b; b = tmp;
Comments
Post a Comment