Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > swap method in Java

Reply
Thread Tools

swap method in Java

 
 
Bill Reyn
Guest
Posts: n/a
 
      06-22-2004
I know we have been over this before, but I still don't get it./*
How can you swap two integers in Java? - in C++ I can swap references
with 3 lines of code - but how on earth am I supposed to do it in
Java, the code below works, but cannot be the right way of doing it.
Why not?
--------------------------------------------------------------------
/* this swaps 2 integers correctly, BUT its seems non-OO */
class Swap4
{
static int x=5, y=7 ; // this looks horrid!
public static void main(String [] args){
WorkItOut w = new WorkItOut();
w.swaps(x,y);
System.out.println( " x (was 5)now is " + x + " y (was 7) now is " +
y);
}
}
class WorkItOut
{
void swaps(int p, int q){
Swap4 s = new Swap4();
s.x = q;
s.y = p; }
}

---------------------------------------------------------------
If your interested in Cplusplus its simply:
void SwapInt( int &nA, int &nB)
{

int nC;
nC = nA;
nA = nB;
nB = nC;

}
 
Reply With Quote
 
 
 
 
Michael Borgwardt
Guest
Posts: n/a
 
      06-22-2004
Bill Reyn wrote:

> I know we have been over this before, but I still don't get it./*
> How can you swap two integers in Java? - in C++ I can swap references
> with 3 lines of code - but how on earth am I supposed to do it in
> Java, the code below works, but cannot be the right way of doing it.
> Why not?


Because you're *not supposed* to do it in Java at all, not the way
you want it to. If you want C, you know where to find it, but if
you want to use Java, don't expect to do things the same way as in C.
(what you want to do would be bad style in C++ as well).

Java does not have C-style pointers, and it's object-oriented.
You don't work with arbitrary variables via pointers. Local
variables are *local*. To make them accessibale in another
method, they have to be parts of an object, i.e. fields or
array elements.
 
Reply With Quote
 
Bryce
Guest
Posts: n/a
 
      06-22-2004
On 22 Jun 2004 04:55:44 -0700, (Bill Reyn)
wrote:

>I know we have been over this before, but I still don't get it./*
>How can you swap two integers in Java? - in C++ I can swap references
>with 3 lines of code - but how on earth am I supposed to do it in
>Java, the code below works, but cannot be the right way of doing it.
>Why not?


Why on earth would you do it that way...

>--------------------------------------------------------------------
>/* this swaps 2 integers correctly, BUT its seems non-OO */
>class Swap4
>{
> static int x=5, y=7 ; // this looks horrid!


Well, you declared them static. I see no reason why.

In Java, you don't have pointers. You have references.

So in short, you cannot swap variables in a method. You would need to
do it inline like this:

public class SwapTest {
public static void main (String[] args) {
Integer x = new Integer(5);
Integer y = new Integer(7);

System.out.println("Before Swap");
System.out.println(" x=[" + x + "]");
System.out.println(" y=[" + y + "]");

Integer tmp = x;
x = y;
y = tmp;

System.out.println("After Swap");
System.out.println(" x=[" + x + "]");
System.out.println(" y=[" + y + "]");
}
}

More information on the differences between C++ and Java and how Java
passes values to functions:
http://www-106.ibm.com/developerwork...y/j-passbyval/


--
now with more cowbell
 
Reply With Quote
 
Joona I Palaste
Guest
Posts: n/a
 
      06-22-2004
Bryce <> scribbled the following:
> On 22 Jun 2004 04:55:44 -0700, (Bill Reyn)
> wrote:


>>I know we have been over this before, but I still don't get it./*
>>How can you swap two integers in Java? - in C++ I can swap references
>>with 3 lines of code - but how on earth am I supposed to do it in
>>Java, the code below works, but cannot be the right way of doing it.
>>Why not?


> Why on earth would you do it that way...


>>--------------------------------------------------------------------
>>/* this swaps 2 integers correctly, BUT its seems non-OO */
>>class Swap4
>>{
>> static int x=5, y=7 ; // this looks horrid!


> Well, you declared them static. I see no reason why.


> In Java, you don't have pointers. You have references.


> So in short, you cannot swap variables in a method. You would need to
> do it inline like this:


> public class SwapTest {
> public static void main (String[] args) {
> Integer x = new Integer(5);
> Integer y = new Integer(7);


> System.out.println("Before Swap");
> System.out.println(" x=[" + x + "]");
> System.out.println(" y=[" + y + "]");
>
> Integer tmp = x;
> x = y;
> y = tmp;
>
> System.out.println("After Swap");
> System.out.println(" x=[" + x + "]");
> System.out.println(" y=[" + y + "]");
> }
> }


Your example above would work just as well if you used:

int x = 5;
int y = 7;

int tmp = x;
x = y;
y = tmp,

If all your variables are local to one method, it doesn't matter whether
they are primitives or references, you can use their values any way you
like.
The problem, which you don't seem to have grasped, comes when these
variables are passed onto other methods.

In C you can do this:

void swap(int *x, int *y) {
int tmp = *x;
*x = *y;
*y = tmp;
}

In Java, you can't. Not with primitive types, anyway. But if you have
some sort of wrapper object, you can do this:

void swap(IntWrapper x, IntWrapper y) {
int tmp = x.getValue();
x.setValue(y.getValue());
y.setValue(tmp);
}

You can't do this with java.lang.Integer as it doesn't have methods to
change its value, but you can write your own wrapper class.

> More information on the differences between C++ and Java and how Java
> passes values to functions:
> http://www-106.ibm.com/developerwork...y/j-passbyval/


I suggest you read that yourself...

--
/-- Joona Palaste () ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"All that flower power is no match for my glower power!"
- Montgomery Burns
 
Reply With Quote
 
Andy Fish
Guest
Posts: n/a
 
      06-22-2004

"Joona I Palaste" <> wrote in message
news:cb9gvd$qv1$...

>
> In C you can do this:
>
> void swap(int *x, int *y) {
> int tmp = *x;
> *x = *y;
> *y = tmp;
> }
>
> In Java, you can't. Not with primitive types, anyway. But if you have
> some sort of wrapper object, you can do this:
>


in java, all parameters are passed by value. that's a decision the language
designers took and I for one am 100% behind them. when you pass a parameter
into a procedure you don't expect that your own variable to be updated by
the procedure.

sometimes it would have been nice to be able to return 2 values from a
function like you can in perl. then you could have a function like

(int, int) swap (int a, int b) {
return (b, a);
}
and call it like this:
(a, b) = swap (a, b);

but the whole idea of passing by reference is something I don't think
accords well with good programming practice.


 
Reply With Quote
 
Joona I Palaste
Guest
Posts: n/a
 
      06-22-2004
Andy Fish <> scribbled the following:
> "Joona I Palaste" <> wrote in message
> news:cb9gvd$qv1$...
>> In C you can do this:
>>
>> void swap(int *x, int *y) {
>> int tmp = *x;
>> *x = *y;
>> *y = tmp;
>> }
>>
>> In Java, you can't. Not with primitive types, anyway. But if you have
>> some sort of wrapper object, you can do this:


> in java, all parameters are passed by value. that's a decision the language
> designers took and I for one am 100% behind them. when you pass a parameter
> into a procedure you don't expect that your own variable to be updated by
> the procedure.


The example I gave, but which you snipped away, was not trying to
disprove that all parameters are passed by value. It was trying to show
that when you pass a reference to an object from one method to another,
the references in both methods refer to the same object.

--
/-- Joona Palaste () ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"When a man talks dirty to a woman, that's sexual harassment. When a woman talks
dirty to a man, that's 14.99 per minute + local telephone charges!"
- Ruben Stiller
 
Reply With Quote
 
Tim Ward
Guest
Posts: n/a
 
      06-22-2004
"Andy Fish" <> wrote in message
news:yWXBc.2788$...
>
> in java, all parameters are passed by value. that's a decision the

language
> designers took and I for one am 100% behind them. when you pass a

parameter
> into a procedure you don't expect that your own variable to be updated by
> the procedure.


Or even a constant. Here's a good one from the days of FORTRAN (I've
probably got the syntax wrong, it's a while since I've done any):

SUBROUTINE NASTY( I )
I = 10
RETURN

CALL NASTY( 6 )
WRITE( 6, 99 )
99 FORMAT( ..... )
END

What happens? NASTY changes the value of its parameter, which is a constant
passed by reference, so every later reference to the constant 6 actually
finds the value 10. So, the debug print line doesn't work, because it tries
to write to unit 10, which probably doesn't exist, instead of unit 6, the
line printer.

(Yes, I discovered this by debugging real code, *not* by trying to construct
a theoretical pathelogical case.)

--
Tim Ward
Brett Ward Limited - www.brettward.co.uk


 
Reply With Quote
 
Thomas Weidenfeller
Guest
Posts: n/a
 
      06-22-2004
Joona I Palaste wrote:
> int x = 5;
> int y = 7;
>
> int tmp = x;
> x = y;
> y = tmp,


From the old days, when memory was scare, and one couldn't afford a
temporary variable or register but could waste some 1MHz CPU cycles

x ^= y;
y ^= x;
x ^= y;

/Thomas
 
Reply With Quote
 
Joona I Palaste
Guest
Posts: n/a
 
      06-22-2004
Thomas Weidenfeller <> scribbled the following:
> Joona I Palaste wrote:
>> int x = 5;
>> int y = 7;
>>
>> int tmp = x;
>> x = y;
>> y = tmp,


> From the old days, when memory was scare, and one couldn't afford a
> temporary variable or register but could waste some 1MHz CPU cycles


> x ^= y;
> y ^= x;
> x ^= y;


Which will only work for unsigned integer values and will still fail
miserably if x and y should ever be the same variable... (as it can
be in Java if they're elements of the same array, and the indices
happen to match).

--
/-- Joona Palaste () ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"This isn't right. This isn't even wrong."
- Wolfgang Pauli
 
Reply With Quote
 
Bryce
Guest
Posts: n/a
 
      06-22-2004
On 22 Jun 2004 14:50:21 GMT, Joona I Palaste <>
wrote:

>Bryce <> scribbled the following:
>
>> public class SwapTest {
>> public static void main (String[] args) {
>> Integer x = new Integer(5);
>> Integer y = new Integer(7);

>
>> System.out.println("Before Swap");
>> System.out.println(" x=[" + x + "]");
>> System.out.println(" y=[" + y + "]");
>>
>> Integer tmp = x;
>> x = y;
>> y = tmp;
>>
>> System.out.println("After Swap");
>> System.out.println(" x=[" + x + "]");
>> System.out.println(" y=[" + y + "]");
>> }
>> }

>
>Your example above would work just as well if you used:
>
>int x = 5;
>int y = 7;
>
>int tmp = x;
>x = y;
>y = tmp,


Yea, and it works just as well the other way.. What's your point?

>If all your variables are local to one method, it doesn't matter whether
>they are primitives or references, you can use their values any way you
>like.


I think that's the point I was trying to make!

>The problem, which you don't seem to have grasped, comes when these
>variables are passed onto other methods.


I grasped it just fine. What makes you think otherwise?

>In C you can do this:
>
>void swap(int *x, int *y) {
> int tmp = *x;
> *x = *y;
> *y = tmp;
>}
>
>In Java, you can't. Not with primitive types, anyway.


And not with the primitive wrappers either... Of course you can write
your own wrapper object, but that wasn't the point of the OP's post.

> But if you have
>some sort of wrapper object, you can do this:
>
>void swap(IntWrapper x, IntWrapper y) {
> int tmp = x.getValue();
> x.setValue(y.getValue());
> y.setValue(tmp);
>}
>
>You can't do this with java.lang.Integer as it doesn't have methods to
>change its value, but you can write your own wrapper class.
>
>> More information on the differences between C++ and Java and how Java
>> passes values to functions:
>> http://www-106.ibm.com/developerwork...y/j-passbyval/

>
>I suggest you read that yourself...



--
now with more cowbell
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Poll: Is a Java Method an Instance of the Java Classjava.lang.reflect.Method? Please reply with YES or NO. Paka Small Java 15 02-07-2012 06:04 PM
generic swap method in javascript proxygeek Javascript 44 06-20-2010 06:35 PM
efficient swap method forums_mp@hotmail.com C++ 9 11-03-2009 08:21 AM
Any fast method to swap two nodes in a STL list? Atlas C++ 10 11-18-2005 04:29 PM
implementing swap for integers in java Madhur Ahuja Java 12 08-10-2004 08:32 AM



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57