Home arrow static arrow Java Programming [Archive] - How can I to write swap()
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - How can I to write swap()
This topic has 13 replies on 1 page.

Posts:13
Registered: 7/26/04
How can I to write swap()   
Aug 1, 2004 8:43 PM



 
There are tow objects named objA,objB.
I want to swap them.
How can I to write the function code to complete!
public void swap(){}

There is a resource:

import java.io.*;

public class TestSwap{
private String str;
public TestSwap(String str){
this.str=str;
}
public void print(){
System.out.println("This is "+this.str);
}

public static void swap(Object objA,Object objB){
Object temp=objA;
objA=objB;
objB=temp;
}

public static void main(String[]args){
TestSwap tsA=new TestSwap("A");
TestSwap tsB=new TestSwap("B");
swap(tsA,tsB);
tsA.print();
tsB.print();

}
}
************************************************************
This is A
This is B
************************************************************
but I execute that program,the objA and objB did not swap;
thanks
 

Posts:1,183
Registered: 1/23/02
Re: How can I to write swap()   
Aug 1, 2004 8:47 PM (reply 1 of 13)



 
Can't be done. Java is a pass-by-value laguage. Implementation of swap() requires pass-by-reference semantics.
 

Posts:8,813
Registered: 10/4/00
Re: How can I to write swap()   
Aug 1, 2004 8:55 PM (reply 2 of 13)



 
Tada!!!
public class TestSwap {  private String str;  public TestSwap(String str) { this.str = str; }  public void print() { System.out.println("This is " + str); }  public static void swap(Object[] objA, Object[] objB) {    Object temp = objA[0];    objA[0] = objB[0];    objB[0] = temp;  }  public static void main(String[] args) {    TestSwap[] tsA = { new TestSwap("A") };    TestSwap[] tsB = { new TestSwap("B") };    swap(tsA, tsB);    tsA[0].print();    tsB[0].print();  }}
PS - This is really dumb.
 

Posts:5,965
Registered: 5/17/03
Re: How can I to write swap()   
Aug 1, 2004 11:11 PM (reply 3 of 13)



 
As has been explained you cannot swap references in a method the way you intended. What you could do instead is to swap the contents of the two objects, for example using an interface like this.
public interface Swapable {   void swap(Object o);}//public class TestSwap implements Swapable {   private String str;   public TestSwap(String str) {      this.str = str;   }   public void swap(Object o) { // swapping of contents      TestSwap that = (TestSwap)o;      String tmp = this.str;      this.str = that.str;      that.str = tmp;   }}//objA.swap(objB); // swapping call

Basically you've gone from swapping references to swapping contents.
 

Posts:5,965
Registered: 5/17/03
Re: How can I to write swap()   
Aug 1, 2004 11:24 PM (reply 4 of 13)



 
Can't be done. Java is a pass-by-value laguage.
Implementation of swap() requires pass-by-reference
semantics.

Can be done because although the parameter passing mechanis in Java is by value, objects have reference sematics. This allows you to do a "value swap" of objects in a method.

See my previous post.
 

Posts:81
Registered: 6/9/04
Re: How can I to write swap()   
Aug 2, 2004 12:19 AM (reply 5 of 13)



 
Passing by reference is a nasty mechanic anyway when combined with changes to the variables you took by reference.

The abstract way it was handled in C which doesnt have pass by reference is passing the value of a pointer to a pointer.

For example
void foo(Object** ob1,Object** ob2);t//hen the caller hasObject* r,s;  //Object is a typedef for a struct//something to load emfoo(&r,&s); //youll note that in cfoo(r,s);//doesnt let you swap r and s for the same reasons you cant in java.


The difference between c and java is that c has that super quick shorthand to create a variable type that has a reference to another variable type. JAva has no such shorthand which is a bummer cause it could have without introducing the concept of memory pointer. In java to explicitly accomplish the same thing as C you have to wrap your class in another class and give it a variable that stores a reference to another variable type arrays for example do exactly that.. But if your going to do that abstractly its usually superior to just store your data in one class and keep that and pass it around. You can add the cool word "Metrics" to the end. In the end half the problem is caused by static procedural code though.

Its worth noting that C++ does have the pass by reference mechanic.
void foo(Object* &o1,Object* &o1);//with sameObject* r,s;foo(r,s);

Now c++ can change r and s to point to different stuff because o1 and o2 in foo are aliases for r and s.

However whats nasty about this notation?
In the C version without pass by reference the caller was explicitly showing that he understood r and s could be changed by passing their address[by value]. In the C++ version the caller doesnt explicitly show that he is aware that r and s can change value by calling foo.

I find the c++ version of true pass by reference to be a tad on the wild side. MAybe its just me. Cs work around is aggressive enough but at the same time a very explicit syntax.


 

Posts:13
Registered: 7/26/04
Re: How can I to write swap()   
Aug 2, 2004 4:37 AM (reply 6 of 13)



 
thanks!
 

Posts:1,183
Registered: 1/23/02
Re: How can I to write swap()   
Aug 2, 2004 6:17 AM (reply 7 of 13)



 
Can be done because although the parameter passing mechanis in Java is by value, objects have reference sematics. This allows you to do a "value swap" of objects in a method.

This is most enlightening, UJ.
 

Posts:447
Registered: 3/8/01
Re: How can I to write swap()   
Aug 2, 2004 6:44 AM (reply 8 of 13)



 
You can do it by just inlining swap:
Object a  = ...;Object b = ...; // ... // swap a and b:Object temp=a; a=b;b=temp;


The reason it doesn't work if you pass a and b to a method is that references are passed by value.
 

Posts:441
Registered: 2/25/04
Re: How can I to write swap()   
Aug 2, 2004 6:54 AM (reply 9 of 13)



 
You can add the cool word "[url http://dictionary.reference.com/search?q=metrics&r=67]Metrics[/url]" to the end.

Or any other 'cool' word if you can't think of a meaning name for your class.

Pete

 

Posts:1,183
Registered: 1/23/02
Re: How can I to write swap()   
Aug 2, 2004 7:52 AM (reply 10 of 13)



 
You can do it by just inlining swap

That's a cheat, no? The significance of swap() is that it's a classic litmus test for languages. If swap() can be implemented then the language supports passs-by-reference semantics.

I like bbrita's cheat the best (reply #2). Very tidy. :-)
 

Posts:5,965
Registered: 5/17/03
Re: How can I to write swap()   
Aug 2, 2004 9:36 AM (reply 11 of 13)



 
If
swap() can be implemented then the language supports
passs-by-reference semantics.

This isn't quite true. You can implement a swap() in C but it doesn't support pass-by-reference. C has pass-by-value only just as Java.
 

Posts:8,813
Registered: 10/4/00
Re: How can I to write swap()   
Aug 2, 2004 9:48 AM (reply 12 of 13)



 
If
swap() can be implemented then the language supports
passs-by-reference semantics.

This isn't quite true. You can implement a swap() in C
but it doesn't support pass-by-reference. C has
pass-by-value only just as Java.

Are there any languages which do support your definition of pass-by-reference?
 

Posts:1,183
Registered: 1/23/02
Re: How can I to write swap()   
Aug 2, 2004 10:40 AM (reply 13 of 13)



 
This isn't quite true. You can implement a swap() in C but it doesn't support pass-by-reference. C has pass-by-value only just as Java.

To second bbritta's comment, any parameter passing mechanism is by-value if you look deep enough. It's all bits and registers at some level.

This is why swap() is important. It demonstrates at a sufficiently high level whether the language supports a mechanism whereby a callee can mutate its caller's local variable. By trying to implement swap() you can show that C++ supports pass-by-reference in the syntax, C requires you to code it explicitly, while Java makes it impossible.
 
This topic has 13 replies on 1 page.