Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2007

Tip: Looking for answers? Try searching our database.

Call By Reference

Thread view: 
ravi - 19 Aug 2007 05:28 GMT
class ABC
{
public static void swap(Integer x,Integer y)
{
 x^=y^=x^=y;
}

public static void main(String args[])
{
 Integer a = 10;
 Integer b = 20;
 swap(a,b);
 System.out.println("a="+a+"\nb="+b);
}
}

O/P
a=10
b=20

Can anybody tell me why this program is not giving correct o/p i.e
a=20
b=10
Thomas Hawtin - 19 Aug 2007 06:26 GMT
>  public static void swap(Integer x,Integer y)
>  {
>   x^=y^=x^=y;
>  }

That is (I think, I wouldn't write code like that!)

    public static void swap(Integer x, Integer y) {
        x = Integer.valueOf(x.intValue() ^ y.intValue());
        y = Integer.valueOf(y.intValue() ^ x.intValue());
        x = Integer.valueOf(x.intValue() ^ y.intValue());
    }

> Can anybody tell me why this program is not giving correct o/p i.e

Anyway, Java is always call by value, even when those values are
references (this makes code very much more easy to follow). Your code is
just changing the references (which are copies), not the objects pointed to.

You could write something similar that changes the object themselves.
Integer is immutable, so you will need a different type.

import java.util.concurrent.atomic.AtomicInteger;
...
    public static void swap(
         final AtomicInteger x, final AtomicInteger y
    ) {
        x.set(x.get() ^ y.get());
        y.set(y.get() ^ x.get());
        x.set(x.get() ^ y.get());
    }

Tom Hawtin
Patricia Shanahan - 19 Aug 2007 06:58 GMT
...
> You could write something similar that changes the object themselves.
> Integer is immutable, so you will need a different type.
[quoted text clipped - 8 lines]
>         x.set(x.get() ^ y.get());
>     }

Also, many uses of swap really exchange elements of the same array:

/* swap elements i and j of array.*/
public static void intArraySwap(int[] array, int i, int j){
  int temp = array[i];
  array[i] = array[j];
  array[j] = temp;
}

Incidentally, in Java code, why do people prefer the xor-swap to using a
temporary? To me, the temporary version seems much more direct and obvious.

Patricia
Thomas Hawtin - 19 Aug 2007 16:49 GMT
> Incidentally, in Java code, why do people prefer the xor-swap to using a
> temporary? To me, the temporary version seems much more direct and obvious.

Because it shows l33t h4x0r ski11z? It made sense in assembler on
pre-RISC processors with few registers. It makes little sense on heavily
optimised processors with large register files. It makes even less sense
in a 3GL.

I suggest to anyone who thinks xor swaps are k3wl, that they work out
their frustrations with a copy of Hacker's Delight and not in their
common or garden production or example code.

http://www.amazon.co.uk/dp/0201914654

Tom Hawtin
Mike Schilling - 19 Aug 2007 18:22 GMT
>> Incidentally, in Java code, why do people prefer the xor-swap to
>> using a temporary? To me, the temporary version seems much more
[quoted text clipped - 4 lines]
> heavily optimised processors with large register files. It makes even
> less sense in a 3GL.

There is a "neat trick" for making a singly-linked list that can be
traversed in both directions by having each "pointer" be the XOR of the
addresses of he previous and next members.  It doesn't work in Java, which
is probably a good thing.  And it breaks conservative garbage collectors in
C++.
Thomas Hawtin - 19 Aug 2007 18:50 GMT
> There is a "neat trick" for making a singly-linked list that can be
> traversed in both directions by having each "pointer" be the XOR of the
> addresses of he previous and next members.  It doesn't work in Java, which
> is probably a good thing.  And it breaks conservative garbage collectors in
> C++.

Now that is a neat trick. You could write impure Java (with
sun.misc.Unsafe for instance) to do it. However, you would still confuse
the garbage collector. I guess you could have an array to store
references for GC, but that would still hinder those GC algorithms that
arrange objects close to referrers (as would a linked list algorithm
that replaced nodes with arrays of references and (xor) indexes to next
'node' - but it makes me want to try it).

Tom Hawtin
Patricia Shanahan - 20 Aug 2007 01:07 GMT
>>> Incidentally, in Java code, why do people prefer the xor-swap to
>>> using a temporary? To me, the temporary version seems much more
[quoted text clipped - 9 lines]
> is probably a good thing.  And it breaks conservative garbage collectors in
> C++.

I'm aware of several situations in which the xor-swap trick is useful.
For example, it can be used to implement swap as a C expression for use
in a pre-processor macro.

I'm trying to find out why people use it in ordinary Java code. I don't
think any of the special cases apply, but maybe I'm missing something.

The other possibility is that users of xor-swap consider it better style
than temp-swap. If so, why?

Patricia
Twisted - 20 Aug 2007 04:02 GMT
> The other possibility is that users of xor-swap consider it better style
> than temp-swap. If so, why?

At a guess, they simply find the use of a temporary, especially an
explicit temporary, to be ugly.
Roedy Green - 22 Aug 2007 12:07 GMT
>The other possibility is that users of xor-swap consider it better style
>than temp-swap. If so, why?

It is magic.  It is cool. It is a way of showing off.
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Roedy Green - 19 Aug 2007 10:19 GMT
> public static void swap(Integer x,Integer y)
> {
>  x^=y^=x^=y;
> }

you can't write a swap routine in Java.

See http://mindprod.com/jgloss/swap.html
http://mindprod.com/jgloss/callbyreference.html
http://mindprod.com/jgloss/callbyvalue.html

Though it is theoretically allowed, chaining operators like than is
just a way to tick people off.  Don't do it.  It's only function is
writing unmaintainable code. See
http://mindprod.com/jgloss/unmain.html
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.