exchange values of two variables
Well we were having a discussion around the water cooler and the topic came up to exchange the values of two variables without using a thirs one. Now I know that you could do that for primitive data types e.g.,
int a = 2;
int b = 3;
a=a+b;
b=a-b;
a=a-b;
But one of my co workers claims that you can exchange values of two String objects without using a third one. Well he has posed this as a challenge. Though I personally don't agree that its possible but wanted to check if one of you is aware of any such algo?
℘
[638 byte] By [
kilyasa] at [2007-11-26 14:35:04]

> Well we were having a discussion around the water
> cooler and the topic came up to exchange the values
> of two variables without using a thirs one. Now I
> know that you could do that for primitive data types
> e.g.,
>
> > int a = 2;
> int b = 3;
> a=a+b;
> b=a-b;
> a=a-b;
>
> But one of my co workers claims that you can exchange
> values of two String objects without using a third
> one. Well he has posed this as a challenge. Though
> I personally don't agree that its possible but wanted
> to check if one of you is aware of any such algo?
>
> ℘
Could the same algorithm not be applied only instead of adding numbers add letters (IE their ascii values)?
Just a question that came to mind without doing any of the calculation work involved.
> Could the same algorithm not be applied only instead
> of adding numbers add letters (IE their ascii
> values)?
>
> Just a question that came to mind without doing any
> of the calculation work involved.
ASCII values are for chars and not Strings?
℘
I think the difficulty in doing this with Strings is that they are objects, not primitives. You can add and subtract primitive values (and even do this with xor, there was a thread about it a few weeks ago), but you can't add or subtract Strings to each other. You could do that to each of the chars separately, but I think you'd have to use a temporary variable to store the intermediate results. I haven't tried it yet, so someone try to prove me wrong.
Have you tried your a+b, a-b, a-b trick with values of a and b sufficiently large that arithmetic overflow will occur? Does it still work? (Not that I really care, I wouldn't ever put that in my code, but if it doesn't work that would be useful for somebody else to know.)
[quote]
Have you tried your a+b, a-b, a-b trick with values of a and b sufficiently large that arithmetic overflow will occur? Does it still work? (Not that I really care, I wouldn't ever put that in my code, but if it doesn't work that would be useful for somebody else to know.)
[/quote]
Interestingly enough, it doesn't seem to matter.
int x = 1333333333;
int y = 1222222222;
System.out.println("1:\t" + x + " - " + y);
x = x+y;
System.out.println("2:\t" + x + " - " + y);
y = x-y;
System.out.println("3:\t" + x + " - " + y);
x = x-y;
System.out.println("4:\t" + x + " - " + y);
~Tim
Message was edited by:
SomeoneElse
Edit, quoted to post I replied to, and changed does to doesn't.
I really need to proofread my posts better.
Tim