Re: variable exchange
- From: Patricia Shanahan <pats@xxxxxxx>
- Date: Tue, 03 Oct 2006 15:15:27 GMT
man4*.* wrote:
hello...
as a beginer in Java I've got a few workbooks for practice, so
here's one example.
-how to exchange content of 2 int variables x and y using only those 2
variables without using
any functions and any other variable?
temp=x;
x=y;
y=temp;
this would be easier...but they have to complicate.... :-)
Get a different book. You already know the simple, clear way of doing
the job. Why use a book that wants to waste your time on a very
specialized trick?
See http://www.caliberdt.com/tips/Apr2003.htm for a description of how
to swap without the temporary variable.
In addition to the limitations they describe, note that it temporarily
assigns one of the variables a value that is neither its old value nor
its new value. That may have implications in some multi-threaded code.
I've known the technique for about 20 years. I have yet to encounter a
situation in which I've needed to use it.
Patricia
.
- Follow-Ups:
- Re: variable exchange
- From: Thomas Weidenfeller
- Re: variable exchange
- From: man4*.*
- Re: variable exchange
- From: Tim Ward
- Re: variable exchange
- References:
- variable exchange
- From: man4*.*
- variable exchange
- Prev by Date: Re: variable exchange
- Next by Date: Re: Reporting tool allowing embedded video.
- Previous by thread: Re: variable exchange
- Next by thread: Re: variable exchange
- Index(es):
Relevant Pages
|